Skip to content

Commit

Permalink
add custom http client test for hystrix
Browse files Browse the repository at this point in the history
  • Loading branch information
darshanime committed Mar 21, 2018
1 parent a9dc05f commit 32ce3b7
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions hystrix_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,3 +407,42 @@ func TestHystrixHTTPClientReturnsFallbackFailureWithAFallBackFunctionWhichReturn
_, err := client.Get("http://foobar.example", http.Header{})
assert.Nil(t, err)
}

func TestCustomHystrixHTTPClientDoSuccess(t *testing.T) {
hystrixCommandConfig := hystrix.CommandConfig{
Timeout: 10,
MaxConcurrentRequests: 100,
ErrorPercentThreshold: 10,
SleepWindow: 100,
RequestVolumeThreshold: 10,
}

timeout := 10 * time.Millisecond

client := NewHystrixHTTPClient(timeout, HystrixConfig{
commandName: "some_new_command_name",
commandConfig: hystrixCommandConfig,
})

client.SetCustomHTTPClient(&myHTTPClient{
client: http.Client{Timeout: 25 * time.Millisecond}})

dummyHandler := func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, r.Header.Get("foo"), "bar")
assert.NotEqual(t, r.Header.Get("foo"), "baz")
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{ "response": "ok" }`))
}

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

req, err := http.NewRequest(http.MethodGet, server.URL, nil)
require.NoError(t, err)
response, err := client.Do(req)
assert.Equal(t, http.StatusOK, response.StatusCode)

body, err := ioutil.ReadAll(response.Body)
require.NoError(t, err)
assert.Equal(t, "{ \"response\": \"ok\" }", string(body))
}

0 comments on commit 32ce3b7

Please sign in to comment.