Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Internal] Add transport to handle request routing #16221

Merged
merged 13 commits into from
Nov 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions sdk/internal/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
# Release History

## 0.8.3 (Unreleased)
## 0.8.3 (2021-11-30)

### Features Added
* Adds `NewRecordingHTTPClient()` method which returns an `azcore.Transporter` interface that routes requests to the test proxy [#16221](https://github.com/Azure/azure-sdk-for-go/pull/16221).
* Adds the `SetBodilessMatcher` method [#16256](https://github.com/Azure/azure-sdk-for-go/pull/16256)
### Breaking Changes

### Breaking Changes
* Renames `ResetSanitizers` to `ResetProxy`

### Other Changes
* Renames `ResetSanitizers` to `ResetProxy` [#16256](https://github.com/Azure/azure-sdk-for-go/pull/16256)

## 0.8.2 (2021-11-11)

Expand Down
27 changes: 27 additions & 0 deletions sdk/internal/recording/recording.go
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,33 @@ func getRootCas(t *testing.T) (*x509.CertPool, error) {
return rootCAs, nil
}

type RecordingHTTPClient struct {
defaultClient *http.Client
options RecordingOptions
t *testing.T
}

func (c RecordingHTTPClient) Do(req *http.Request) (*http.Response, error) {
c.options.ReplaceAuthority(c.t, req)
return c.defaultClient.Do(req)
}

func NewRecordingHTTPClient(t *testing.T, options *RecordingOptions) (*RecordingHTTPClient, error) {
if options == nil {
options = &RecordingOptions{UseHTTPS: true}
}
c, err := GetHTTPClient(t)
if err != nil {
return nil, err
}

return &RecordingHTTPClient{
defaultClient: c,
options: *options,
t: t,
}, nil
}

func GetHTTPClient(t *testing.T) (*http.Client, error) {
transport := http.DefaultTransport.(*http.Transport).Clone()

Expand Down
43 changes: 43 additions & 0 deletions sdk/internal/recording/recording_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package recording

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
Expand Down Expand Up @@ -433,6 +434,48 @@ func TestStartStop(t *testing.T) {
defer jsonFile.Close()
}

func TestStartStopRecordingClient(t *testing.T) {
temp := recordMode
recordMode = RecordingMode
defer func() { recordMode = temp }()

err := Start(t, packagePath, nil)
require.NoError(t, err)

client, err := NewRecordingHTTPClient(t, nil)
require.NoError(t, err)

req, err := http.NewRequest("POST", "https://azsdkengsys.azurecr.io/acr/v1/some_registry/_tags", nil)
require.NoError(t, err)

resp, err := client.Do(req)
require.NoError(t, err)
require.NotNil(t, resp)

require.NotNil(t, GetRecordingId(t))

err = Stop(t, nil)
require.NoError(t, err)

// Make sure the file is there
jsonFile, err := os.Open(fmt.Sprintf("./testdata/recordings/%s.json", t.Name()))
require.NoError(t, err)
defer func() {
err = jsonFile.Close()
require.NoError(t, err)
err = os.Remove(jsonFile.Name())
require.NoError(t, err)
}()

var data RecordingFileStruct
byteValue, err := ioutil.ReadAll(jsonFile)
require.NoError(t, err)
err = json.Unmarshal(byteValue, &data)
require.NoError(t, err)
require.Equal(t, "https://azsdkengsys.azurecr.io/acr/v1/some_registry/_tags", data.Entries[0].RequestURI)
require.Equal(t, req.URL.String(), "https://localhost:5001/acr/v1/some_registry/_tags")
}

func TestProxyCert(t *testing.T) {
_, err := getRootCas(t)
require.NoError(t, err)
Expand Down
14 changes: 7 additions & 7 deletions sdk/internal/recording/sanitizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,13 @@ type RecordingFileStruct struct {
}

type Entry struct {
RequestURI string `json:"RequestUri"`
RequestMethod string `json:"RequestMethod"`
RequestHeaders map[string]string `json:"RequestHeaders"`
RequestBody string `json:"RequestBody"`
StatusCode int `json:"StatusCode"`
ResponseBody interface{} `json:"ResponseBody"` // This should be a string, but proxy saves as an object when there is no body
ResponseHeaders map[string]string `json:"ResponseHeaders"`
RequestURI string `json:"RequestUri"`
RequestMethod string `json:"RequestMethod"`
RequestHeaders map[string]string `json:"RequestHeaders"`
RequestBody string `json:"RequestBody"`
StatusCode int `json:"StatusCode"`
ResponseBody interface{} `json:"ResponseBody"` // This should be a string, but proxy saves as an object when there is no body
ResponseHeaders map[string]interface{} `json:"ResponseHeaders"`
}

func (e Entry) ResponseBodyByValue(k string) interface{} {
Expand Down

This file was deleted.

This file was deleted.