Skip to content

Commit

Permalink
Merge 42d9a8d into d70d8c8
Browse files Browse the repository at this point in the history
  • Loading branch information
amit-lamba committed Feb 25, 2021
2 parents d70d8c8 + 42d9a8d commit 95f3c33
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
6 changes: 5 additions & 1 deletion runtime/server_http_response.go
Expand Up @@ -24,6 +24,7 @@ import (
"context"
"fmt"
"net/http"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -291,7 +292,9 @@ func (res *ServerHTTPResponse) flush(ctx context.Context) {

res.flushed = true
res.writeHeader(res.pendingStatusCode)
res.writeBytes(res.pendingBodyBytes)
if res.pendingStatusCode != http.StatusNoContent {
res.writeBytes(res.pendingBodyBytes)
}
res.finish(ctx)
}

Expand All @@ -308,6 +311,7 @@ func (res *ServerHTTPResponse) writeBytes(bytes []byte) {
res.contextLogger.Error(res.Request.Context(),
"Could not write string to resp body",
zap.Error(err),
zap.String("bytesLength", strconv.Itoa(len(bytes))),
)
}
}
Expand Down
65 changes: 65 additions & 0 deletions runtime/server_http_response_test.go
Expand Up @@ -520,6 +520,71 @@ func TestPendingResponseBody(t *testing.T) {
)
}

func TestPendingResponseBody204StatusNoContent(t *testing.T) {
gateway, err := benchGateway.CreateGateway(
defaultTestConfig,
defaultTestOptions,
exampleGateway.CreateGateway,
)

if !assert.NoError(t, err) {
return
}
defer gateway.Close()

bgateway := gateway.(*benchGateway.BenchGateway)
deps := createDefaultDependencies(bgateway)
err = bgateway.ActualGateway.HTTPRouter.Handle(
"GET", "/foo", http.HandlerFunc(zanzibar.NewRouterEndpoint(
bgateway.ActualGateway.ContextExtractor,
deps,
"foo", "foo",
func(
ctx context.Context,
req *zanzibar.ServerHTTPRequest,
res *zanzibar.ServerHTTPResponse,
) {
obj := &MyBody{
Token: "myToken",
Client: MyBodyClient{
Token: "myClientToken",
},
}
bytes, err := json.Marshal(obj)
statusCode := 204
assert.NoError(t, err)
res.WriteJSON(statusCode, nil, obj)

pendingBytes, pendingStatusCode := res.GetPendingResponse()
assert.Equal(t, bytes, pendingBytes)
assert.Equal(t, statusCode, pendingStatusCode)

headers := res.Headers()
assert.NotNil(t, headers)
},
).HandleRequest),
)
assert.NoError(t, err)

resp, err := gateway.MakeRequest("GET", "/foo", nil, nil)
if !assert.NoError(t, err) {
return
}

assert.Equal(t, resp.StatusCode, 204)
bytes, err := ioutil.ReadAll(resp.Body)
if !assert.NoError(t, err) {
return
}

//The body would become blank
assert.Equal(
t,
"",
string(bytes),
)
}

func TestPendingResponseObject(t *testing.T) {
gateway, err := benchGateway.CreateGateway(
defaultTestConfig,
Expand Down

0 comments on commit 95f3c33

Please sign in to comment.