-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add logging of http request with GitHub
Signed-off-by: Marco Franssen <marco.franssen@philips.com>
- Loading branch information
1 parent
f70badb
commit 7225285
Showing
4 changed files
with
137 additions
and
11 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,21 @@ | ||
package transport | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"net/http" | ||
) | ||
|
||
// TeeRoundTripper copies request bodies to stdout. | ||
type TeeRoundTripper struct { | ||
http.RoundTripper | ||
Writer io.Writer | ||
} | ||
|
||
// RoundTrip executes a single HTTP transaction, returning | ||
// a Response for the provided Request. | ||
func (t TeeRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { | ||
fmt.Fprintf(t.Writer, "%s: %s\n", req.Method, req.URL) | ||
|
||
return t.RoundTripper.RoundTrip(req) | ||
} |
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,45 @@ | ||
package transport_test | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/philips-labs/slsa-provenance-action/lib/transport" | ||
) | ||
|
||
func TestTeeRoundTripper(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
fmt.Fprintln(w, "Hello world") | ||
})) | ||
defer ts.Close() | ||
|
||
var writer strings.Builder | ||
client := http.Client{ | ||
Transport: transport.TeeRoundTripper{ | ||
RoundTripper: http.DefaultTransport, | ||
Writer: &writer, | ||
}, | ||
} | ||
|
||
jsonString := `{ "say": "hello-world", "to": "marco" }` | ||
json := strings.NewReader(jsonString) | ||
_, err := client.Post(ts.URL, "application/json", json) | ||
|
||
assert.NoError(err) | ||
assert.NotEmpty(writer.String()) | ||
assert.Equal(fmt.Sprintf("POST: %s\n", ts.URL), writer.String()) | ||
|
||
writer.Reset() | ||
_, err = client.Get(ts.URL) | ||
|
||
assert.NoError(err) | ||
assert.NotEmpty(writer.String()) | ||
assert.Equal(fmt.Sprintf("GET: %s\n", ts.URL), writer.String()) | ||
} |