Skip to content

Commit

Permalink
support http head
Browse files Browse the repository at this point in the history
  • Loading branch information
Wito Chandra committed Nov 8, 2020
1 parent 2f1fd27 commit 21d6e57
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 0 deletions.
12 changes: 12 additions & 0 deletions httpclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,18 @@ func (c *Client) Delete(url string, headers http.Header) (*http.Response, error)
return c.Do(request)
}

// Head makes a HTTP HEAD request with provided URL
func (c *Client) Head(url string, headers http.Header) (*http.Response, error) {
request, err := http.NewRequest(http.MethodHead, url, nil)
if err != nil {
return nil, errors.Wrap(err, "HEAD - request creation failed")
}

request.Header = headers

return c.Do(request)
}

// Do makes an HTTP request with the native `http.Do` interface
func (c *Client) Do(request *http.Request) (*http.Response, error) {
request.Close = true
Expand Down
26 changes: 26 additions & 0 deletions httpclient/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,32 @@ func TestHTTPClientDeleteSuccess(t *testing.T) {
assert.Equal(t, "{ \"response\": \"ok\" }", respBody(t, response))
}

func TestHTTPClientHeadSuccess(t *testing.T) {
client := NewClient(WithHTTPTimeout(10 * time.Millisecond))

dummyHandler := func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodHead, r.Method)
assert.Equal(t, "application/json", r.Header.Get("Content-Type"))
assert.Equal(t, "en", r.Header.Get("Accept-Language"))

w.Header().Add("x-foo", "bar")
w.WriteHeader(http.StatusOK)
}

server := httptest.NewServer(http.HandlerFunc(dummyHandler))
defer server.Close()

headers := http.Header{}
headers.Set("Content-Type", "application/json")
headers.Set("Accept-Language", "en")

response, err := client.Head(server.URL, headers)
require.NoError(t, err, "should not have failed to make a HEAD request")

assert.Equal(t, http.StatusOK, response.StatusCode)
assert.Equal(t, "bar", response.Header.Get("x-foo"))
}

func TestHTTPClientPutSuccess(t *testing.T) {
client := NewClient(WithHTTPTimeout(10 * time.Millisecond))

Expand Down
12 changes: 12 additions & 0 deletions hystrix/hystrix_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,18 @@ func (hhc *Client) Delete(url string, headers http.Header) (*http.Response, erro
return hhc.Do(request)
}

// Head makes a HTTP HEAD request with provided URL
func (hhc *Client) Head(url string, headers http.Header) (*http.Response, error) {
request, err := http.NewRequest(http.MethodHead, url, nil)
if err != nil {
return nil, errors.Wrap(err, "HEAD - request creation failed")
}

request.Header = headers

return hhc.Do(request)
}

// Do makes an HTTP request with the native `http.Do` interface
func (hhc *Client) Do(request *http.Request) (*http.Response, error) {
var response *http.Response
Expand Down
34 changes: 34 additions & 0 deletions hystrix/hystrix_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,40 @@ func TestHystrixHTTPClientDeleteSuccess(t *testing.T) {
assert.Equal(t, "{ \"response\": \"ok\" }", respBody(t, response))
}

func TestHystrixHTTPClientHeadSuccess(t *testing.T) {
client := NewClient(
WithHTTPTimeout(10*time.Millisecond),
WithCommandName("head_command"),
WithHystrixTimeout(10*time.Millisecond),
WithMaxConcurrentRequests(100),
WithErrorPercentThreshold(10),
WithSleepWindow(100),
WithRequestVolumeThreshold(10),
)

dummyHandler := func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodHead, r.Method)
assert.Equal(t, "application/json", r.Header.Get("Content-Type"))
assert.Equal(t, "en", r.Header.Get("Accept-Language"))

w.Header().Add("x-foo", "bar")
w.WriteHeader(http.StatusOK)
}

server := httptest.NewServer(http.HandlerFunc(dummyHandler))
defer server.Close()

headers := http.Header{}
headers.Set("Content-Type", "application/json")
headers.Set("Accept-Language", "en")

response, err := client.Head(server.URL, headers)
require.NoError(t, err, "should not have failed to make a HEAD request")

assert.Equal(t, http.StatusOK, response.StatusCode)
assert.Equal(t, "bar", response.Header.Get("x-foo"))
}

func TestHystrixHTTPClientPutSuccess(t *testing.T) {
client := NewClient(
WithHTTPTimeout(10*time.Millisecond),
Expand Down

0 comments on commit 21d6e57

Please sign in to comment.