Skip to content

Commit

Permalink
tests: add tests for thrift exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Raynos committed Apr 20, 2017
1 parent ee5cd52 commit b81fc14
Showing 1 changed file with 114 additions and 0 deletions.
114 changes: 114 additions & 0 deletions test/endpoints/bar/bar_normal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,117 @@ func TestBarNormalMalformedClientResponseReadAll(t *testing.T) {
assert.Equal(t, string(respBytes),
`{"error":"Unexpected server error"}`)
}

func TestBarExceptionCode(t *testing.T) {
var counter int = 0

gateway, err := testGateway.CreateGateway(t, nil, &testGateway.Options{
KnownHTTPBackends: []string{"bar"},
TestBinary: filepath.Join(
getDirName(), "..", "..", "..",
"examples", "example-gateway", "build", "main.go",
),
})
if !assert.NoError(t, err, "got bootstrap err") {
return
}
defer gateway.Close()

gateway.HTTPBackends()["bar"].HandleFunc(
"POST", "/bar-path",
func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(403)
if _, err := w.Write([]byte("{}")); err != nil {
t.Fatal("can't write fake response")
}
counter++
},
)

res, err := gateway.MakeRequest(
"POST", "/bar/bar-path", nil,
bytes.NewReader([]byte(`{"stringField":"foo"}`)),
)
if !assert.NoError(t, err, "got http error") {
return
}

assert.Equal(t, "403 Forbidden", res.Status)
assert.Equal(t, 1, counter)
}

func TestMalformedBarExceptionCode(t *testing.T) {
var counter int = 0

gateway, err := testGateway.CreateGateway(t, nil, &testGateway.Options{
KnownHTTPBackends: []string{"bar"},
TestBinary: filepath.Join(
getDirName(), "..", "..", "..",
"examples", "example-gateway", "build", "main.go",
),
})
if !assert.NoError(t, err, "got bootstrap err") {
return
}
defer gateway.Close()

gateway.HTTPBackends()["bar"].HandleFunc(
"POST", "/bar-path",
func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(403)
if _, err := w.Write([]byte("")); err != nil {
t.Fatal("can't write fake response")
}
counter++
},
)

res, err := gateway.MakeRequest(
"POST", "/bar/bar-path", nil,
bytes.NewReader([]byte(`{"stringField":"foo"}`)),
)
if !assert.NoError(t, err, "got http error") {
return
}

assert.Equal(t, "500 Internal Server Error", res.Status)
assert.Equal(t, 1, counter)
}

func TestBarExceptionInvalidStatusCode(t *testing.T) {
var counter int = 0

gateway, err := testGateway.CreateGateway(t, nil, &testGateway.Options{
KnownHTTPBackends: []string{"bar"},
TestBinary: filepath.Join(
getDirName(), "..", "..", "..",
"examples", "example-gateway", "build", "main.go",
),
})
if !assert.NoError(t, err, "got bootstrap err") {
return
}
defer gateway.Close()

gateway.HTTPBackends()["bar"].HandleFunc(
"POST", "/bar-path",
func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(402)
if _, err := w.Write([]byte("{}")); err != nil {
t.Fatal("can't write fake response")
}
counter++
},
)

res, err := gateway.MakeRequest(
"POST", "/bar/bar-path", nil,
bytes.NewReader([]byte(`{"stringField":"foo"}`)),
)
if !assert.NoError(t, err, "got http error") {
return
}

assert.Equal(t, "500 Internal Server Error", res.Status)
assert.Equal(t, 1, counter)
}

0 comments on commit b81fc14

Please sign in to comment.