Skip to content

Commit

Permalink
Fix Go style issues in package httputil (bazelbuild#263)
Browse files Browse the repository at this point in the history
  • Loading branch information
fweikert authored Aug 12, 2021
1 parent ca10ec4 commit 0f006d8
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
4 changes: 4 additions & 0 deletions httputil/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@ import (
"net/http"
)

// FakeTransport represents a fake http.Transport that returns prerecorded responses.
type FakeTransport struct {
responses map[string]*responseCollection
}

// NewFakeTransport creates a new FakeTransport instance without any responses.
func NewFakeTransport() *FakeTransport {
return &FakeTransport{
responses: make(map[string]*responseCollection),
}
}

// AddResponse stores a fake HTTP response for the given URL.
func (ft *FakeTransport) AddResponse(url string, status int, body string, headers map[string]string) {
if _, ok := ft.responses[url]; !ok {
ft.responses[url] = &responseCollection{}
Expand All @@ -24,6 +27,7 @@ func (ft *FakeTransport) AddResponse(url string, status int, body string, header
ft.responses[url].Add(createResponse(status, body, headers))
}

// RoundTrip returns a prerecorded response to the given request, if one exists. Otherwise its response indicates 404 - not found.
func (ft *FakeTransport) RoundTrip(req *http.Request) (*http.Response, error) {
if responses, ok := ft.responses[req.URL.String()]; ok {
return responses.Next(), nil
Expand Down
1 change: 1 addition & 0 deletions httputil/httputil.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var (
retryHeaders = []string{"Retry-After", "X-RateLimit-Reset", "Rate-Limit-Reset"}
)

// Clock keeps track of time. It can return the current time, as well as move forward by sleeping for a certain period.
type Clock interface {
Sleep(time.Duration)
Now() time.Time
Expand Down

0 comments on commit 0f006d8

Please sign in to comment.