Skip to content

Commit

Permalink
delay response
Browse files Browse the repository at this point in the history
  • Loading branch information
Jesse Michael committed Apr 14, 2018
1 parent 514778e commit d34f5ab
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 7 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Go-Rest-Assured keeps track of the Assured Calls you have stubbed out and the Ca
- Response
- Headers
- Query
- Delay
- Callbacks

Set these fields as a *Given* call through the client or a HTTP request to the service directly and they will be returned from the Go Rest Assured API when you hit the *When* endpoint. The Calls you stub out are uniquely mapped with an identity of their Method and Path. If you stub multiple calls to the same Method and Path, the responses will cycle through your stubs based on the order they were created.
Expand Down Expand Up @@ -54,13 +55,16 @@ The Request Body, if present, will be stored in the Assured Call

The stored Status Code will be `200 OK` unless you specify a `"Assured-Status": "[0-9]+"` HTTP Header

You can also set a response delay with the HTTP Header `Assured-Delay` with a number of seconds

Or..

```go
call := assured.Call{
Path: "test/assured",
StatusCode: 201,
Method: "GET",
Delay: 2,
}
// Stub out an assured call
client.Given(call)
Expand Down
1 change: 1 addition & 0 deletions assured/bindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

const (
AssuredStatus = "Assured-Status"
AssuredDelay = "Assured-Delay"
AssuredCallbackKey = "Assured-Callback-Key"
AssuredCallbackTarget = "Assured-Callback-Target"
AssuredCallbackDelay = "Assured-Callback-Delay"
Expand Down
1 change: 1 addition & 0 deletions assured/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Call struct {
Path string `json:"path"`
Method string `json:"method"`
StatusCode int `json:"status_code"`
Delay int `json:"delay"`
Headers map[string]string `json:"headers"`
Query map[string]string `json:"query,omitempty"`
Response CallResponse `json:"response,omitempty"`
Expand Down
5 changes: 4 additions & 1 deletion assured/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ func (c *Client) Given(calls ...Call) error {
return err
}
if call.StatusCode != 0 {
req.Header.Set(AssuredStatus, fmt.Sprintf("%d", call.StatusCode))
req.Header.Set(AssuredStatus, strconv.Itoa(call.StatusCode))
}
if call.Delay > 0 {
req.Header.Set(AssuredDelay, strconv.Itoa(call.Delay))
}
for key, value := range call.Headers {
req.Header.Set(key, value)
Expand Down
5 changes: 4 additions & 1 deletion assured/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ func TestClientCallbacks(t *testing.T) {
require.NoError(t, client.Given(Call{
Path: "test/assured",
Method: "POST",
Delay: 2,
Callbacks: []Callback{
Callback{
Method: "POST",
Expand All @@ -151,7 +152,7 @@ func TestClientCallbacks(t *testing.T) {
Callback{
Method: "POST",
Target: delayTestServer.URL,
Delay: 2,
Delay: 4,
Response: []byte(`{"wait":"there's more"}`),
},
},
Expand All @@ -160,9 +161,11 @@ func TestClientCallbacks(t *testing.T) {
req, err := http.NewRequest(http.MethodPost, client.URL()+"/test/assured", bytes.NewReader([]byte(`{"calling":"here"}`)))
require.NoError(t, err)

start := time.Now()
_, err = httpClient.Do(req)
require.NoError(t, err)

require.True(t, time.Since(start) >= 2*time.Second, "response should be delayed 2 seconds")
// allow go routine to finish
time.Sleep(1 * time.Second)
require.True(t, called, "callback was not hit")
Expand Down
5 changes: 5 additions & 0 deletions assured/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ func (a *AssuredEndpoints) WhenEndpoint(ctx context.Context, call *Call) (interf
go a.sendCallback(callback.Headers[AssuredCallbackTarget], callback)
}

// Delay response
if delay, err := strconv.ParseInt(assured.Headers[AssuredDelay], 10, 64); err == nil {
time.Sleep(time.Duration(delay) * time.Second)
}

a.logger.Log("message", "assured call responded", "path", call.ID())
return assured, nil
}
Expand Down
8 changes: 5 additions & 3 deletions assured/endpoints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,16 +214,17 @@ func TestWhenEndpointSuccessCallbacks(t *testing.T) {
require.True(t, called, "callback was not hit")
}

func TestWhenEndpointSuccessCallbacksDelayed(t *testing.T) {
func TestWhenEndpointSuccessDelayed(t *testing.T) {
called := false
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
called = true
}))
assured := testCall1()
assured.Headers[AssuredCallbackKey] = "call-key"
assured.Headers[AssuredDelay] = "2"
call := testCallback()
call.Headers[AssuredCallbackTarget] = testServer.URL
call.Headers[AssuredCallbackDelay] = "2"
call.Headers[AssuredCallbackDelay] = "4"
endpoints := &AssuredEndpoints{
logger: testSettings.Logger,
assuredCalls: &CallStore{
Expand All @@ -235,9 +236,10 @@ func TestWhenEndpointSuccessCallbacksDelayed(t *testing.T) {
},
trackMadeCalls: true,
}

start := time.Now()
c, err := endpoints.WhenEndpoint(ctx, assured)

require.True(t, time.Since(start) >= 2*time.Second, "response should be delayed 2 seconds")
require.NoError(t, err)
require.Equal(t, assured, c)
// allow go routine to finish
Expand Down
3 changes: 3 additions & 0 deletions testdata/callbacks.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"path": "test/assured",
"method": "GET",
"status_code": 201,
"delay": 0,
"response": "testdata/assured.json",
"headers": {
"Content-Length": "17",
Expand All @@ -14,6 +15,7 @@
"path": "test/assured",
"method": "GET",
"status_code": 200,
"delay": 0,
"response": "testdata/image.jpg",
"headers": {
"Content-Length": "56000",
Expand All @@ -25,6 +27,7 @@
"path": "teapot/assured",
"method": "POST",
"status_code": 418,
"delay": 2,
"headers": {
"Content-Length": "0",
"User-Agent": "Go-http-client/1.1",
Expand Down
7 changes: 5 additions & 2 deletions testdata/calls.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"path": "test/assured",
"method": "GET",
"status_code": 200,
"response": "{\"assured\": true}",
"delay": 0,
"response": "eyJhc3N1cmVkIjogdHJ1ZX0=",
"headers": {
"Content-Length": "17",
"User-Agent": "Go-http-client/1.1",
Expand All @@ -17,7 +18,8 @@
"path": "test/assured",
"method": "GET",
"status_code": 409,
"response": "error",
"delay": 0,
"response": "ZXJyb3I=",
"headers": {
"Content-Length": "5",
"User-Agent": "Go-http-client/1.1",
Expand All @@ -28,6 +30,7 @@
"path": "teapot/assured",
"method": "POST",
"status_code": 418,
"delay": 0,
"headers": {
"Content-Length": "0",
"User-Agent": "Go-http-client/1.1",
Expand Down

0 comments on commit d34f5ab

Please sign in to comment.