-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
51565a7
commit 3aea883
Showing
3 changed files
with
186 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package utils | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestDumpTransport(t *testing.T) { | ||
reqTestCases := []struct { | ||
description string | ||
path string | ||
headers map[string]string | ||
body string | ||
assert func(t *testing.T, req *http.Request, dump string) | ||
}{ | ||
{ | ||
description: "Only Authz header is redacted", | ||
headers: map[string]string{ | ||
"Authorization": "some-token", | ||
"Other-Header": "other-value", | ||
}, | ||
assert: func(t *testing.T, req *http.Request, dump string) { | ||
assert.NotContains(t, dump, "some-token") | ||
assert.Contains(t, dump, "other-value") | ||
|
||
assert.Equal(t, req.Header.Get("Authorization"), "some-token") | ||
assert.Equal(t, req.Header.Get("Other-Header"), "other-value") | ||
}, | ||
}, | ||
{ | ||
description: "Request body is redacted on authentication requests", | ||
path: "/authn-xyz/account/login", | ||
body: "some-body", | ||
assert: func(t *testing.T, req *http.Request, dump string) { | ||
assert.NotContains(t, dump, "some-body") | ||
|
||
reqBody, err := ioutil.ReadAll(req.Body) | ||
assert.Nil(t, err) | ||
assert.Equal(t, string(reqBody), "some-body") | ||
}, | ||
}, | ||
{ | ||
description: "Request body is maintained on other requests", | ||
body: "some-body", | ||
assert: func(t *testing.T, req *http.Request, dump string) { | ||
assert.Contains(t, dump, "some-body") | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range reqTestCases { | ||
t.Run(tc.description, func(t *testing.T) { | ||
req, err := http.NewRequest( | ||
"POST", | ||
fmt.Sprintf("http://somehost.com%s", tc.path), | ||
bytes.NewBuffer([]byte(tc.body)), | ||
) | ||
assert.Nil(t, err) | ||
for k, v := range tc.headers { | ||
req.Header.Add(k, v) | ||
} | ||
|
||
dump := NewDumpTransport(nil, nil).dumpRequest(req) | ||
tc.assert(t, req, string(dump)) | ||
}) | ||
} | ||
|
||
respTestCases := []struct { | ||
description string | ||
body string | ||
assert func(t *testing.T, res *http.Response, dump string) | ||
}{ | ||
{ | ||
description: "Body is redacted if it contains a Conjur token", | ||
body: "{\"protected\":\"abcde\",\"payload\":\"fghijk\",\"signature\":\"lmnop\"}", | ||
assert: func(t *testing.T, res *http.Response, dump string) { | ||
assert.NotContains(t, dump, "{\"protected\":\"abcde\",\"payload\":\"fghijk\",\"signature\":\"lmnop\"}") | ||
|
||
reqBody, err := ioutil.ReadAll(res.Body) | ||
assert.Nil(t, err) | ||
assert.Contains(t, string(reqBody), "{\"protected\":\"abcde\",\"payload\":\"fghijk\",\"signature\":\"lmnop\"}") | ||
}, | ||
}, | ||
{ | ||
description: "Body is maintained otherwise", | ||
body: "some-body", | ||
assert: func(t *testing.T, res *http.Response, dump string) { | ||
assert.Contains(t, dump, "some-body") | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range respTestCases { | ||
t.Run(tc.description, func(t *testing.T) { | ||
resp := http.Response{ | ||
Body: ioutil.NopCloser(bytes.NewBufferString(tc.body)), | ||
} | ||
|
||
dump := NewDumpTransport(nil, nil).dumpResponse(&resp) | ||
tc.assert(t, &resp, string(dump)) | ||
}) | ||
} | ||
} |