Repository: Azure/azure-sdk-for-go
Pull request #15657 (2021-09-28 15:31:07)

Close initial LRO response body when creating a poller
<!--
Thank you for contributing to the Azure SDK for Go.

Please verify the following before submitting your PR, thank you!
-->

- [ ] The purpose of this PR is explained in this or a referenced issue.
- [ ] The PR does not update generated files.
   - These files are managed by the codegen framework at [Azure/autorest.go][].
- [ ] Tests are included and/or updated for code changes.
- [ ] Updates to [CHANGELOG.md][] are included.
- [ ] MIT license headers are included in each file.

[Azure/autorest.go]: https://github.com/Azure/autorest.go
[CHANGELOG.md]: https://github.com/Azure/azure-sdk-for-go/blob/main/CHANGELOG.md



Patch:
diff --git a/sdk/azcore/CHANGELOG.md b/sdk/azcore/CHANGELOG.md
index f0bcb22b4a..20ad188037 100644
--- a/sdk/azcore/CHANGELOG.md
+++ b/sdk/azcore/CHANGELOG.md
@@ -7,6 +7,7 @@
 
 ### Bug Fixes
 * Fixed a potential panic when creating the default Transporter.
+* Close LRO initial response body when creating a poller.
 
 ## 0.19.0 (2021-08-25)
 
diff --git a/sdk/azcore/arm/runtime/poller.go b/sdk/azcore/arm/runtime/poller.go
index f4e6df175b..b4c0b3ae2b 100644
--- a/sdk/azcore/arm/runtime/poller.go
+++ b/sdk/azcore/arm/runtime/poller.go
@@ -23,6 +23,7 @@ import (
 // NewPoller creates a Poller based on the provided initial response.
 // pollerID - a unique identifier for an LRO.  it's usually the client.Method string.
 func NewPoller(pollerID string, finalState string, resp *http.Response, pl pipeline.Pipeline, eu func(*http.Response) error) (*pollers.Poller, error) {
+	defer resp.Body.Close()
 	// this is a back-stop in case the swagger is incorrect (i.e. missing one or more status codes for success).
 	// ideally the codegen should return an error if the initial response failed and not even create a poller.
 	if !pollers.StatusCodeValid(resp) {
diff --git a/sdk/azcore/go.mod b/sdk/azcore/go.mod
index 0fa6902d34..df297fe33b 100644
--- a/sdk/azcore/go.mod
+++ b/sdk/azcore/go.mod
@@ -1,7 +1,7 @@
 module github.com/Azure/azure-sdk-for-go/sdk/azcore
 
 require (
-	github.com/Azure/azure-sdk-for-go/sdk/internal v0.7.0
+	github.com/Azure/azure-sdk-for-go/sdk/internal v0.7.1
 	github.com/stretchr/testify v1.7.0
 	golang.org/x/net v0.0.0-20210610132358-84b48f89b13b
 )
diff --git a/sdk/azcore/go.sum b/sdk/azcore/go.sum
index 99e6b012a9..d1b1f79eee 100644
--- a/sdk/azcore/go.sum
+++ b/sdk/azcore/go.sum
@@ -1,27 +1,34 @@
-github.com/Azure/azure-sdk-for-go/sdk/internal v0.7.0 h1:v9p9TfTbf7AwNb5NYQt7hI41IfPoLFiFkLtb+bmGjT0=
-github.com/Azure/azure-sdk-for-go/sdk/internal v0.7.0/go.mod h1:yqy467j36fJxcRV2TzfVZ1pCb5vxm4BtZPUdYWe/Xo8=
+github.com/Azure/azure-sdk-for-go/sdk/internal v0.7.1 h1:8XSiy/LSvjtFwpguk7m6yGLgGkWocluo8hLM5vtcpcg=
+github.com/Azure/azure-sdk-for-go/sdk/internal v0.7.1/go.mod h1:KLF4gFr6DcKFZwSuH8w8yEK6DpFl3LP5rhdvAb7Yz5I=
+github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
-github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
-github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
-github.com/dnaeon/go-vcr v1.2.0/go.mod h1:R4UdLID7HZT3taECzJs4YgbbH6PIGXB6W/sc5OLb6RQ=
+github.com/dnaeon/go-vcr v1.1.0/go.mod h1:M7tiix8f0r6mKKJ3Yq/kqU1OYf3MnfmBWVbPx/yU9ko=
 github.com/modocache/gover v0.0.0-20171022184752-b58185e213c5/go.mod h1:caMODM3PzxT8aQXRPkAt8xlV/e7d7w8GM5g0fa5F0D8=
 github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
 github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
 github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
 github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
 github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
+golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
+golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
+golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
+golang.org/x/net v0.0.0-20201010224723-4f7140c49acb/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
 golang.org/x/net v0.0.0-20210610132358-84b48f89b13b h1:k+E048sYJHyVnsr1GDrRZWQ32D2C7lWs9JRc0bel53A=
 golang.org/x/net v0.0.0-20210610132358-84b48f89b13b/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
+golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
+golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
 golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
 golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
 golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
+golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
+golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
 golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
 golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
 golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
 gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
 gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
-gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
+gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
 gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
+gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
 gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
-gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
-gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
diff --git a/sdk/azcore/runtime/poller.go b/sdk/azcore/runtime/poller.go
index 686c04725d..35a715ad7a 100644
--- a/sdk/azcore/runtime/poller.go
+++ b/sdk/azcore/runtime/poller.go
@@ -22,6 +22,7 @@ import (
 // NewPoller creates a Poller based on the provided initial response.
 // pollerID - a unique identifier for an LRO, it's usually the client.Method string.
 func NewPoller(pollerID string, resp *http.Response, pl pipeline.Pipeline, eu func(*http.Response) error) (*pollers.Poller, error) {
+	defer resp.Body.Close()
 	// this is a back-stop in case the swagger is incorrect (i.e. missing one or more status codes for success).
 	// ideally the codegen should return an error if the initial response failed and not even create a poller.
 	if !pollers.StatusCodeValid(resp) {
diff --git a/sdk/data/azcosmos/cosmos_container_properties.go b/sdk/data/azcosmos/cosmos_container_properties.go
index 0be31741ab..afa4568694 100644
--- a/sdk/data/azcosmos/cosmos_container_properties.go
+++ b/sdk/data/azcosmos/cosmos_container_properties.go
@@ -3,12 +3,14 @@
 
 package azcosmos
 
+import "github.com/Azure/azure-sdk-for-go/sdk/azcore"
+
 // CosmosContainerProperties represents the properties of a container.
 type CosmosContainerProperties struct {
 	// Id contains the unique id of the container.
 	Id string `json:"id"`
 	// ETag contains the entity etag of the container.
-	ETag string `json:"_etag,omitempty"`
+	ETag azcore.ETag `json:"_etag,omitempty"`
 	// SelfLink contains the self-link of the container.
 	SelfLink string `json:"_self,omitempty"`
 	// ResourceId contains the resource id of the container.
diff --git a/sdk/data/azcosmos/cosmos_database_properties.go b/sdk/data/azcosmos/cosmos_database_properties.go
index e303947b71..8b793a48cf 100644
--- a/sdk/data/azcosmos/cosmos_database_properties.go
+++ b/sdk/data/azcosmos/cosmos_database_properties.go
@@ -3,12 +3,14 @@
 
 package azcosmos
 
+import "github.com/Azure/azure-sdk-for-go/sdk/azcore"
+
 // CosmosDatabaseProperties represents the properties of a database.
 type CosmosDatabaseProperties struct {
 	// Id contains the unique id of the database.
 	Id string `json:"id"`
 	// ETag contains the entity etag of the database
-	ETag string `json:"_etag,omitempty"`
+	ETag azcore.ETag `json:"_etag,omitempty"`
 	// SelfLink contains the self-link of the database
 	SelfLink string `json:"_self,omitempty"`
 	// ResourceId contains the resource id of the database
diff --git a/sdk/data/azcosmos/cosmos_database_request_options.go b/sdk/data/azcosmos/cosmos_database_request_options.go
index ae9c365643..592de96e35 100644
--- a/sdk/data/azcosmos/cosmos_database_request_options.go
+++ b/sdk/data/azcosmos/cosmos_database_request_options.go
@@ -3,23 +3,25 @@
 
 package azcosmos
 
+import "github.com/Azure/azure-sdk-for-go/sdk/azcore"
+
 // CosmosDatabaseRequestOptions includes options for operations against a database.
 type CosmosDatabaseRequestOptions struct {
-	IfMatchEtag     string
-	IfNoneMatchEtag string
+	IfMatchEtag     *azcore.ETag
+	IfNoneMatchEtag *azcore.ETag
 }
 
 func (options *CosmosDatabaseRequestOptions) toHeaders() *map[string]string {
-	if options.IfMatchEtag == "" && options.IfNoneMatchEtag == "" {
+	if options.IfMatchEtag == nil && options.IfNoneMatchEtag == nil {
 		return nil
 	}
 
 	headers := make(map[string]string)
-	if options.IfMatchEtag != "" {
-		headers[headerIfMatch] = options.IfMatchEtag
+	if options.IfMatchEtag != nil {
+		headers[headerIfMatch] = string(*options.IfMatchEtag)
 	}
-	if options.IfNoneMatchEtag != "" {
-		headers[headerIfNoneMatch] = options.IfNoneMatchEtag
+	if options.IfNoneMatchEtag != nil {
+		headers[headerIfNoneMatch] = string(*options.IfNoneMatchEtag)
 	}
 	return &headers
 }
diff --git a/sdk/data/azcosmos/cosmos_item_request_options.go b/sdk/data/azcosmos/cosmos_item_request_options.go
index d584b6b6f9..6e33b01bc2 100644
--- a/sdk/data/azcosmos/cosmos_item_request_options.go
+++ b/sdk/data/azcosmos/cosmos_item_request_options.go
@@ -3,7 +3,11 @@
 
 package azcosmos
 
-import "strings"
+import (
+	"strings"
+
+	"github.com/Azure/azure-sdk-for-go/sdk/azcore"
+)
 
 // CosmosItemRequestOptions includes options for operations on items.
 type CosmosItemRequestOptions struct {
@@ -27,6 +31,9 @@ type CosmosItemRequestOptions struct {
 	// When EnableContentResponseOnWrite is false will cause the response on write operations to have a null resource. This reduces networking and CPU load by not sending the resource back over the network and serializing it on the client.
 	// The default is false.
 	EnableContentResponseOnWrite bool
+	// IfMatchEtag is used to ensure optimistic concurrency control.
+	// https://docs.microsoft.com/azure/cosmos-db/sql/database-transactions-optimistic-concurrency#optimistic-concurrency-control
+	IfMatchEtag *azcore.ETag
 }
 
 func (options *CosmosItemRequestOptions) toHeaders() *map[string]string {
@@ -52,5 +59,9 @@ func (options *CosmosItemRequestOptions) toHeaders() *map[string]string {
 		headers[cosmosHeaderSessionToken] = options.SessionToken
 	}
 
+	if options.IfMatchEtag != nil {
+		headers[headerIfMatch] = string(*options.IfMatchEtag)
+	}
+
 	return &headers
 }
diff --git a/sdk/data/azcosmos/cosmos_response.go b/sdk/data/azcosmos/cosmos_response.go
index 87d646d3e1..d503105779 100644
--- a/sdk/data/azcosmos/cosmos_response.go
+++ b/sdk/data/azcosmos/cosmos_response.go
@@ -6,6 +6,8 @@ package azcosmos
 import (
 	"net/http"
 	"strconv"
+
+	"github.com/Azure/azure-sdk-for-go/sdk/azcore"
 )
 
 // CosmosResponse is the base response type for all responses from the Azure Cosmos DB database service.
@@ -34,6 +36,6 @@ func (c *CosmosResponse) ActivityId() string {
 }
 
 // ETag contains the value from the ETag header.
-func (c *CosmosResponse) ETag() string {
-	return c.RawResponse.Header.Get(cosmosHeaderEtag)
+func (c *CosmosResponse) ETag() azcore.ETag {
+	return azcore.ETag(c.RawResponse.Header.Get(cosmosHeaderEtag))
 }
diff --git a/sdk/data/azcosmos/throughput_properties.go b/sdk/data/azcosmos/throughput_properties.go
index 601e2c5b58..6009c15202 100644
--- a/sdk/data/azcosmos/throughput_properties.go
+++ b/sdk/data/azcosmos/throughput_properties.go
@@ -9,6 +9,7 @@ import (
 	"fmt"
 	"strconv"
 
+	"github.com/Azure/azure-sdk-for-go/sdk/azcore"
 	"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
 )
 
@@ -18,7 +19,7 @@ const (
 
 // ThroughputProperties describes the throughput configuration of a resource.
 type ThroughputProperties struct {
-	ETag         string
+	ETag         azcore.ETag
 	LastModified *UnixTime
 
 	version         string


Test patch:
diff --git a/sdk/azcore/arm/runtime/poller_test.go b/sdk/azcore/arm/runtime/poller_test.go
index 6ac0429eeb..2b065e61d1 100644
--- a/sdk/azcore/arm/runtime/poller_test.go
+++ b/sdk/azcore/arm/runtime/poller_test.go
@@ -9,7 +9,6 @@ package runtime
 import (
 	"context"
 	"io"
-	"io/ioutil"
 	"net/http"
 	"reflect"
 	"strings"
@@ -64,17 +63,18 @@ func handleError(resp *http.Response) error {
 	return me
 }
 
-func initialResponse(method, u string, resp io.Reader) *http.Response {
+func initialResponse(method, u string, resp io.Reader) (*http.Response, mock.TrackedClose) {
 	req, err := http.NewRequest(method, u, nil)
 	if err != nil {
 		panic(err)
 	}
+	body, closed := mock.NewTrackedCloser(resp)
 	return &http.Response{
-		Body:          ioutil.NopCloser(resp),
+		Body:          body,
 		ContentLength: -1,
 		Header:        http.Header{},
 		Request:       req,
-	}
+	}, closed
 }
 
 func TestNewPollerAsync(t *testing.T) {
@@ -83,7 +83,7 @@ func TestNewPollerAsync(t *testing.T) {
 	srv.AppendResponse(mock.WithBody([]byte(statusInProgress)))
 	srv.AppendResponse(mock.WithBody([]byte(statusSucceeded)))
 	srv.AppendResponse(mock.WithBody([]byte(successResp)))
-	resp := initialResponse(http.MethodPut, srv.URL(), strings.NewReader(provStateStarted))
+	resp, closed := initialResponse(http.MethodPut, srv.URL(), strings.NewReader(provStateStarted))
 	resp.Header.Set(shared.HeaderAzureAsync, srv.URL())
 	resp.StatusCode = http.StatusCreated
 	pl := getPipeline(srv)
@@ -91,6 +91,9 @@ func TestNewPollerAsync(t *testing.T) {
 	if err != nil {
 		t.Fatal(err)
 	}
+	if !closed() {
+		t.Fatal("initial response body wasn't closed")
+	}
 	if pt := pollers.PollerType(poller); pt != reflect.TypeOf(&async.Poller{}) {
 		t.Fatalf("unexpected poller type %s", pt.String())
 	}
@@ -117,13 +120,16 @@ func TestNewPollerBody(t *testing.T) {
 	defer close()
 	srv.AppendResponse(mock.WithBody([]byte(provStateUpdating)), mock.WithHeader("Retry-After", "1"))
 	srv.AppendResponse(mock.WithBody([]byte(provStateSucceeded)))
-	resp := initialResponse(http.MethodPatch, srv.URL(), strings.NewReader(provStateStarted))
+	resp, closed := initialResponse(http.MethodPatch, srv.URL(), strings.NewReader(provStateStarted))
 	resp.StatusCode = http.StatusCreated
 	pl := getPipeline(srv)
 	poller, err := NewPoller("pollerID", "", resp, pl, handleError)
 	if err != nil {
 		t.Fatal(err)
 	}
+	if !closed() {
+		t.Fatal("initial response body wasn't closed")
+	}
 	if pt := pollers.PollerType(poller); pt != reflect.TypeOf(&body.Poller{}) {
 		t.Fatalf("unexpected poller type %s", pt.String())
 	}
@@ -150,7 +156,7 @@ func TestNewPollerLoc(t *testing.T) {
 	defer close()
 	srv.AppendResponse(mock.WithStatusCode(http.StatusAccepted))
 	srv.AppendResponse(mock.WithBody([]byte(successResp)))
-	resp := initialResponse(http.MethodPatch, srv.URL(), strings.NewReader(provStateStarted))
+	resp, closed := initialResponse(http.MethodPatch, srv.URL(), strings.NewReader(provStateStarted))
 	resp.Header.Set(shared.HeaderLocation, srv.URL())
 	resp.StatusCode = http.StatusAccepted
 	pl := getPipeline(srv)
@@ -158,6 +164,9 @@ func TestNewPollerLoc(t *testing.T) {
 	if err != nil {
 		t.Fatal(err)
 	}
+	if !closed() {
+		t.Fatal("initial response body wasn't closed")
+	}
 	if pt := pollers.PollerType(poller); pt != reflect.TypeOf(&loc.Poller{}) {
 		t.Fatalf("unexpected poller type %s", pt.String())
 	}
@@ -185,7 +194,7 @@ func TestNewPollerInitialRetryAfter(t *testing.T) {
 	srv.AppendResponse(mock.WithBody([]byte(statusInProgress)))
 	srv.AppendResponse(mock.WithBody([]byte(statusSucceeded)))
 	srv.AppendResponse(mock.WithBody([]byte(successResp)))
-	resp := initialResponse(http.MethodPut, srv.URL(), strings.NewReader(provStateStarted))
+	resp, closed := initialResponse(http.MethodPut, srv.URL(), strings.NewReader(provStateStarted))
 	resp.Header.Set(shared.HeaderAzureAsync, srv.URL())
 	resp.Header.Set("Retry-After", "1")
 	resp.StatusCode = http.StatusCreated
@@ -194,6 +203,9 @@ func TestNewPollerInitialRetryAfter(t *testing.T) {
 	if err != nil {
 		t.Fatal(err)
 	}
+	if !closed() {
+		t.Fatal("initial response body wasn't closed")
+	}
 	if pt := pollers.PollerType(poller); pt != reflect.TypeOf(&async.Poller{}) {
 		t.Fatalf("unexpected poller type %s", pt.String())
 	}
@@ -212,7 +224,7 @@ func TestNewPollerCanceled(t *testing.T) {
 	defer close()
 	srv.AppendResponse(mock.WithBody([]byte(statusInProgress)))
 	srv.AppendResponse(mock.WithBody([]byte(statusCanceled)), mock.WithStatusCode(http.StatusOK))
-	resp := initialResponse(http.MethodPut, srv.URL(), strings.NewReader(provStateStarted))
+	resp, closed := initialResponse(http.MethodPut, srv.URL(), strings.NewReader(provStateStarted))
 	resp.Header.Set(shared.HeaderAzureAsync, srv.URL())
 	resp.StatusCode = http.StatusCreated
 	pl := getPipeline(srv)
@@ -220,6 +232,9 @@ func TestNewPollerCanceled(t *testing.T) {
 	if err != nil {
 		t.Fatal(err)
 	}
+	if !closed() {
+		t.Fatal("initial response body wasn't closed")
+	}
 	if pt := pollers.PollerType(poller); pt != reflect.TypeOf(&async.Poller{}) {
 		t.Fatalf("unexpected poller type %s", pt.String())
 	}
@@ -238,7 +253,7 @@ func TestNewPollerFailedWithError(t *testing.T) {
 	defer close()
 	srv.AppendResponse(mock.WithBody([]byte(statusInProgress)))
 	srv.AppendResponse(mock.WithBody([]byte(errorResp)), mock.WithStatusCode(http.StatusBadRequest))
-	resp := initialResponse(http.MethodPut, srv.URL(), strings.NewReader(provStateStarted))
+	resp, closed := initialResponse(http.MethodPut, srv.URL(), strings.NewReader(provStateStarted))
 	resp.Header.Set(shared.HeaderAzureAsync, srv.URL())
 	resp.StatusCode = http.StatusCreated
 	pl := getPipeline(srv)
@@ -246,6 +261,9 @@ func TestNewPollerFailedWithError(t *testing.T) {
 	if err != nil {
 		t.Fatal(err)
 	}
+	if !closed() {
+		t.Fatal("initial response body wasn't closed")
+	}
 	if pt := pollers.PollerType(poller); pt != reflect.TypeOf(&async.Poller{}) {
 		t.Fatalf("unexpected poller type %s", pt.String())
 	}
@@ -264,13 +282,16 @@ func TestNewPollerSuccessNoContent(t *testing.T) {
 	defer close()
 	srv.AppendResponse(mock.WithBody([]byte(provStateUpdating)))
 	srv.AppendResponse(mock.WithStatusCode(http.StatusNoContent))
-	resp := initialResponse(http.MethodPatch, srv.URL(), strings.NewReader(provStateStarted))
+	resp, closed := initialResponse(http.MethodPatch, srv.URL(), strings.NewReader(provStateStarted))
 	resp.StatusCode = http.StatusCreated
 	pl := getPipeline(srv)
 	poller, err := NewPoller("pollerID", "", resp, pl, handleError)
 	if err != nil {
 		t.Fatal(err)
 	}
+	if !closed() {
+		t.Fatal("initial response body wasn't closed")
+	}
 	if pt := pollers.PollerType(poller); pt != reflect.TypeOf(&body.Poller{}) {
 		t.Fatalf("unexpected poller type %s", pt.String())
 	}
@@ -295,13 +316,16 @@ func TestNewPollerSuccessNoContent(t *testing.T) {
 func TestNewPollerFail202NoHeaders(t *testing.T) {
 	srv, close := mock.NewServer()
 	defer close()
-	resp := initialResponse(http.MethodDelete, srv.URL(), http.NoBody)
+	resp, closed := initialResponse(http.MethodDelete, srv.URL(), http.NoBody)
 	resp.StatusCode = http.StatusAccepted
 	pl := getPipeline(srv)
 	poller, err := NewPoller("pollerID", "", resp, pl, handleError)
 	if err == nil {
 		t.Fatal("unexpected nil error")
 	}
+	if !closed() {
+		t.Fatal("initial response body wasn't closed")
+	}
 	if poller != nil {
 		t.Fatal("expected nil poller")
 	}
diff --git a/sdk/azcore/runtime/poller_test.go b/sdk/azcore/runtime/poller_test.go
index b7d69a1afe..097caf59c9 100644
--- a/sdk/azcore/runtime/poller_test.go
+++ b/sdk/azcore/runtime/poller_test.go
@@ -41,7 +41,9 @@ type widget struct {
 }
 
 func TestNewPollerFail(t *testing.T) {
+	body, closed := mock.NewTrackedCloser(http.NoBody)
 	p, err := NewPoller("fake.poller", &http.Response{
+		Body:       body,
 		StatusCode: http.StatusBadRequest,
 	}, NewPipeline(nil), errUnmarshall)
 	if err == nil {
@@ -50,6 +52,9 @@ func TestNewPollerFail(t *testing.T) {
 	if p != nil {
 		t.Fatal("expected nil poller")
 	}
+	if !closed() {
+		t.Fatal("initial response body wasn't closed")
+	}
 }
 
 func TestNewPollerFromResumeTokenFail(t *testing.T) {
@@ -91,11 +96,16 @@ func TestLocPollerSimple(t *testing.T) {
 			"Retry-After": []string{"1"},
 		},
 	}
+	body, closed := mock.NewTrackedCloser(http.NoBody)
+	firstResp.Body = body
 	pl := NewPipeline(srv)
 	lro, err := NewPoller("fake.poller", firstResp, pl, errUnmarshall)
 	if err != nil {
 		t.Fatal(err)
 	}
+	if !closed() {
+		t.Fatal("initial response body wasn't closed")
+	}
 	resp, err := lro.PollUntilDone(context.Background(), 5*time.Millisecond, nil)
 	if err != nil {
 		t.Fatal(err)
@@ -119,11 +129,16 @@ func TestLocPollerWithWidget(t *testing.T) {
 			"Retry-After": []string{"1"},
 		},
 	}
+	body, closed := mock.NewTrackedCloser(http.NoBody)
+	firstResp.Body = body
 	pl := NewPipeline(srv)
 	lro, err := NewPoller("fake.poller", firstResp, pl, errUnmarshall)
 	if err != nil {
 		t.Fatal(err)
 	}
+	if !closed() {
+		t.Fatal("initial response body wasn't closed")
+	}
 	var w widget
 	resp, err := lro.PollUntilDone(context.Background(), 5*time.Millisecond, &w)
 	if err != nil {
@@ -151,11 +166,16 @@ func TestLocPollerCancelled(t *testing.T) {
 			"Retry-After": []string{"1"},
 		},
 	}
+	body, closed := mock.NewTrackedCloser(http.NoBody)
+	firstResp.Body = body
 	pl := NewPipeline(srv)
 	lro, err := NewPoller("fake.poller", firstResp, pl, errUnmarshall)
 	if err != nil {
 		t.Fatal(err)
 	}
+	if !closed() {
+		t.Fatal("initial response body wasn't closed")
+	}
 	var w widget
 	resp, err := lro.PollUntilDone(context.Background(), 5*time.Millisecond, &w)
 	if err == nil {
@@ -186,11 +206,16 @@ func TestLocPollerWithError(t *testing.T) {
 			"Retry-After": []string{"1"},
 		},
 	}
+	body, closed := mock.NewTrackedCloser(http.NoBody)
+	firstResp.Body = body
 	pl := NewPipeline(srv)
 	lro, err := NewPoller("fake.poller", firstResp, pl, errUnmarshall)
 	if err != nil {
 		t.Fatal(err)
 	}
+	if !closed() {
+		t.Fatal("initial response body wasn't closed")
+	}
 	var w widget
 	resp, err := lro.PollUntilDone(context.Background(), 5*time.Millisecond, &w)
 	if err == nil {
@@ -221,11 +246,16 @@ func TestLocPollerWithResumeToken(t *testing.T) {
 			"Retry-After": []string{"1"},
 		},
 	}
+	body, closed := mock.NewTrackedCloser(http.NoBody)
+	firstResp.Body = body
 	pl := NewPipeline(srv)
 	lro, err := NewPoller("fake.poller", firstResp, pl, errUnmarshall)
 	if err != nil {
 		t.Fatal(err)
 	}
+	if !closed() {
+		t.Fatal("initial response body wasn't closed")
+	}
 	resp, err := lro.Poll(context.Background())
 	if err != nil {
 		t.Fatal(err)
@@ -273,11 +303,16 @@ func TestLocPollerWithTimeout(t *testing.T) {
 			"Location": []string{srv.URL()},
 		},
 	}
+	body, closed := mock.NewTrackedCloser(http.NoBody)
+	firstResp.Body = body
 	pl := NewPipeline(srv)
 	lro, err := NewPoller("fake.poller", firstResp, pl, errUnmarshall)
 	if err != nil {
 		t.Fatal(err)
 	}
+	if !closed() {
+		t.Fatal("initial response body wasn't closed")
+	}
 	ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
 	resp, err := lro.PollUntilDone(ctx, 5*time.Millisecond, nil)
 	cancel()
@@ -300,8 +335,9 @@ func TestOpPollerSimple(t *testing.T) {
 	if err != nil {
 		t.Fatal(err)
 	}
+	body, closed := mock.NewTrackedCloser(http.NoBody)
 	firstResp := &http.Response{
-		Body:       http.NoBody,
+		Body:       body,
 		StatusCode: http.StatusAccepted,
 		Header: http.Header{
 			"Operation-Location": []string{srv.URL()},
@@ -317,6 +353,9 @@ func TestOpPollerSimple(t *testing.T) {
 	if err != nil {
 		t.Fatal(err)
 	}
+	if !closed() {
+		t.Fatal("initial response body wasn't closed")
+	}
 	resp, err := lro.PollUntilDone(context.Background(), 5*time.Millisecond, nil)
 	if err != nil {
 		t.Fatal(err)
@@ -339,8 +378,9 @@ func TestOpPollerWithWidgetPUT(t *testing.T) {
 	if err != nil {
 		t.Fatal(err)
 	}
+	body, closed := mock.NewTrackedCloser(http.NoBody)
 	firstResp := &http.Response{
-		Body:       http.NoBody,
+		Body:       body,
 		StatusCode: http.StatusAccepted,
 		Header: http.Header{
 			"Operation-Location": []string{srv.URL()},
@@ -356,6 +396,9 @@ func TestOpPollerWithWidgetPUT(t *testing.T) {
 	if err != nil {
 		t.Fatal(err)
 	}
+	if !closed() {
+		t.Fatal("initial response body wasn't closed")
+	}
 	var w widget
 	resp, err := lro.PollUntilDone(context.Background(), 5*time.Millisecond, &w)
 	if err != nil {
@@ -382,8 +425,9 @@ func TestOpPollerWithWidgetPOSTLocation(t *testing.T) {
 	if err != nil {
 		t.Fatal(err)
 	}
+	body, closed := mock.NewTrackedCloser(http.NoBody)
 	firstResp := &http.Response{
-		Body:       http.NoBody,
+		Body:       body,
 		StatusCode: http.StatusAccepted,
 		Header: http.Header{
 			"Operation-Location": []string{srv.URL()},
@@ -400,6 +444,9 @@ func TestOpPollerWithWidgetPOSTLocation(t *testing.T) {
 	if err != nil {
 		t.Fatal(err)
 	}
+	if !closed() {
+		t.Fatal("initial response body wasn't closed")
+	}
 	var w widget
 	resp, err := lro.PollUntilDone(context.Background(), 5*time.Millisecond, &w)
 	if err != nil {
@@ -425,8 +472,9 @@ func TestOpPollerWithWidgetPOST(t *testing.T) {
 	if err != nil {
 		t.Fatal(err)
 	}
+	body, closed := mock.NewTrackedCloser(http.NoBody)
 	firstResp := &http.Response{
-		Body:       http.NoBody,
+		Body:       body,
 		StatusCode: http.StatusAccepted,
 		Header: http.Header{
 			"Operation-Location": []string{srv.URL()},
@@ -442,6 +490,9 @@ func TestOpPollerWithWidgetPOST(t *testing.T) {
 	if err != nil {
 		t.Fatal(err)
 	}
+	if !closed() {
+		t.Fatal("initial response body wasn't closed")
+	}
 	var w widget
 	resp, err := lro.PollUntilDone(context.Background(), 5*time.Millisecond, &w)
 	if err != nil {
@@ -469,8 +520,9 @@ func TestOpPollerWithWidgetResourceLocation(t *testing.T) {
 	if err != nil {
 		t.Fatal(err)
 	}
+	body, closed := mock.NewTrackedCloser(http.NoBody)
 	firstResp := &http.Response{
-		Body:       http.NoBody,
+		Body:       body,
 		StatusCode: http.StatusAccepted,
 		Header: http.Header{
 			"Operation-Location": []string{srv.URL()},
@@ -487,6 +539,9 @@ func TestOpPollerWithWidgetResourceLocation(t *testing.T) {
 	if err != nil {
 		t.Fatal(err)
 	}
+	if !closed() {
+		t.Fatal("initial response body wasn't closed")
+	}
 	var w widget
 	resp, err := lro.PollUntilDone(context.Background(), 5*time.Millisecond, &w)
 	if err != nil {
@@ -511,8 +566,9 @@ func TestOpPollerWithResumeToken(t *testing.T) {
 	if err != nil {
 		t.Fatal(err)
 	}
+	body, closed := mock.NewTrackedCloser(http.NoBody)
 	firstResp := &http.Response{
-		Body:       http.NoBody,
+		Body:       body,
 		StatusCode: http.StatusAccepted,
 		Header: http.Header{
 			"Operation-Location": []string{srv.URL()},
@@ -528,6 +584,9 @@ func TestOpPollerWithResumeToken(t *testing.T) {
 	if err != nil {
 		t.Fatal(err)
 	}
+	if !closed() {
+		t.Fatal("initial response body wasn't closed")
+	}
 	resp, err := lro.Poll(context.Background())
 	if err != nil {
 		t.Fatal(err)
@@ -566,11 +625,16 @@ func TestNopPoller(t *testing.T) {
 	firstResp := &http.Response{
 		StatusCode: http.StatusOK,
 	}
+	body, closed := mock.NewTrackedCloser(http.NoBody)
+	firstResp.Body = body
 	pl := NewPipeline(nil)
 	lro, err := NewPoller("fake.poller", firstResp, pl, errUnmarshall)
 	if err != nil {
 		t.Fatal(err)
 	}
+	if !closed() {
+		t.Fatal("initial response body wasn't closed")
+	}
 	if pt := pollers.PollerType(lro); pt != reflect.TypeOf(&pollers.NopPoller{}) {
 		t.Fatalf("unexpected poller type %s", pt.String())
 	}
diff --git a/sdk/data/azcosmos/cosmos_database_request_options_test.go b/sdk/data/azcosmos/cosmos_database_request_options_test.go
index 799c3dffff..36850e8c25 100644
--- a/sdk/data/azcosmos/cosmos_database_request_options_test.go
+++ b/sdk/data/azcosmos/cosmos_database_request_options_test.go
@@ -5,6 +5,8 @@ package azcosmos
 
 import (
 	"testing"
+
+	"github.com/Azure/azure-sdk-for-go/sdk/azcore"
 )
 
 func TestDatabaseRequestOptionsToHeaders(t *testing.T) {
@@ -13,18 +15,20 @@ func TestDatabaseRequestOptionsToHeaders(t *testing.T) {
 		t.Error("toHeaders should return nil")
 	}
 
-	options.IfMatchEtag = "etag"
-	options.IfNoneMatchEtag = "noneetag"
+	etagValue := azcore.ETag("etag")
+	noneEtagValue := azcore.ETag("noneetag")
+	options.IfMatchEtag = &etagValue
+	options.IfNoneMatchEtag = &noneEtagValue
 	header := options.toHeaders()
 	if header == nil {
 		t.Error("toHeaders should return non-nil")
 	}
 
 	headers := *header
-	if headers[headerIfMatch] != options.IfMatchEtag {
+	if headers[headerIfMatch] != string(*options.IfMatchEtag) {
 		t.Errorf("IfMatchEtag not set matching expected %v got %v", options.IfMatchEtag, headers[headerIfMatch])
 	}
-	if headers[headerIfNoneMatch] != options.IfNoneMatchEtag {
+	if headers[headerIfNoneMatch] != string(*options.IfNoneMatchEtag) {
 		t.Errorf("IfNoneMatchEtag not set matching expected %v got %v", options.IfNoneMatchEtag, headers[headerIfNoneMatch])
 	}
 }
diff --git a/sdk/data/azcosmos/cosmos_item_request_options_test.go b/sdk/data/azcosmos/cosmos_item_request_options_test.go
index 6e491cccdc..c6b028b286 100644
--- a/sdk/data/azcosmos/cosmos_item_request_options_test.go
+++ b/sdk/data/azcosmos/cosmos_item_request_options_test.go
@@ -5,6 +5,8 @@ package azcosmos
 
 import (
 	"testing"
+
+	"github.com/Azure/azure-sdk-for-go/sdk/azcore"
 )
 
 func TestItemRequestOptionsToHeaders(t *testing.T) {
@@ -14,6 +16,8 @@ func TestItemRequestOptionsToHeaders(t *testing.T) {
 	options.ConsistencyLevel = ConsistencyLevelSession.ToPtr()
 	options.SessionToken = "sessionToken"
 	options.IndexingDirective = IndexingDirectiveInclude.ToPtr()
+	etagValue := azcore.ETag("someEtag")
+	options.IfMatchEtag = &etagValue
 	header := options.toHeaders()
 	if header == nil {
 		t.Error("toHeaders should return non-nil")
@@ -35,4 +39,7 @@ func TestItemRequestOptionsToHeaders(t *testing.T) {
 	if headers[cosmosHeaderSessionToken] != "sessionToken" {
 		t.Errorf("SessionToken should be sessionToken but got %v", headers[cosmosHeaderSessionToken])
 	}
+	if headers[headerIfMatch] != string(*options.IfMatchEtag) {
+		t.Errorf("IfMatchEtag should be someEtag but got %v", headers[headerIfMatch])
+	}
 }