Skip to content

Commit

Permalink
Merge pull request #11 from linkernetworks/hwchiu/add-testing
Browse files Browse the repository at this point in the history
Add more testing for src/net package

Former-commit-id: a538a72367908d4ccd65075c51150895b2671175 [formerly a538a72367908d4ccd65075c51150895b2671175 [formerly a4b499e]]
Former-commit-id: 97a29d99102caa6f167169365c04a42459b4a035
Former-commit-id: d273571
  • Loading branch information
Hung-Wei Chiu authored Jun 20, 2018
2 parents d7f7045 + aa4510c commit fe8c2d4
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 49 deletions.
37 changes: 37 additions & 0 deletions src/net/http/handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package http

import (
"testing"

"github.com/stretchr/testify/assert"
"net/http"

"github.com/linkernetworks/vortex/src/web"
)

func TestCompositeServiceHandler(t *testing.T) {
data := 0
var handler = func(*web.NativeContext) {
data += 1
}

req, err := http.NewRequest("POST", "http://here.com/v1/signin", nil)
assert.NoError(t, err)

routeHandler := CompositeServiceHandler(nil, handler)
assert.Equal(t, 0, data)
routeHandler(nil, req)
assert.Equal(t, 1, data)
}

func TestRESTfulServiceHandler(t *testing.T) {
data := 0
var handler = func(*web.Context) {
data += 1
}

routeHandler := RESTfulServiceHandler(nil, handler)
assert.Equal(t, 0, data)
routeHandler(nil, nil)
assert.Equal(t, 1, data)
}
101 changes: 67 additions & 34 deletions src/net/http/response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ package http

import (
"errors"
"fmt"
"net/http"
"net/http/httptest"
"testing"

"encoding/json"
"encoding/xml"

"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -41,52 +39,87 @@ func ExampleJsonEncoding() {
InternalServerError(request, recorder, errors.New("Failed to do something"))
}

func TestDefaultErrorXmlEncode(t *testing.T) {
recorder := httptest.NewRecorder()

request, err := http.NewRequest("POST", "http://here.com/v1/signin", nil)
request.Header.Set("content-type", "application/xml")
assert.NoError(t, err)

msg := "Failed to do something"
func TestEncodeErrorPayload(t *testing.T) {
testCases := []struct {
cases string
contentType string
expType string
expMessage string
}{
{"json", "text/json", "text/json", `{"error":false,"message":""}`},
{"xml", "text/xml", "text/xml", `<response><error>false</error><message></message></response>`},
{"default", "", "application/json", `{"error":false,"message":""}`},
}

wl, err := InternalServerError(request, recorder, errors.New(msg))
assert.NoError(t, err)
assert.True(t, wl > 0)
for _, tc := range testCases {
t.Run(tc.cases, func(t *testing.T) {
errPayload := ErrorPayload{}

contentType := recorder.Header().Get("content-type")
assert.Equal(t, "application/xml", contentType)
request, err := http.NewRequest("POST", "http://here.com/v1/signin", nil)
assert.NoError(t, err)

payload := ErrorPayload{}
out := recorder.Body.Bytes()
err = xml.Unmarshal(out, &payload)
assert.NoError(t, err)
request.Header.Set("Content-Type", tc.contentType)
out, cType, err := EncodeErrorPayload(request, errPayload)
assert.Equal(t, tc.expType, cType)
assert.NoError(t, err)
fmt.Printf("%s", out)
assert.Equal(t, tc.expMessage, string(out[:len(out)]))
})
}
}

t.Logf("XML response: %s", out)
func TestNewErrorPayload(t *testing.T) {
errs := []error{
fmt.Errorf("Error One"),
fmt.Errorf("Error Two"),
}

assert.True(t, payload.Error)
assert.Len(t, payload.Message, len(msg))
err := NewErrorPayload(errs[0], errs[1])
assert.Equal(t, errs[0].Error(), err.Message)
assert.Equal(t, errs[1].Error(), err.PreviousMessage)
}

func TestDefaultErrorJsonEncode(t *testing.T) {
func TestWriteStatusAndError(t *testing.T) {
recorder := httptest.NewRecorder()

request, err := http.NewRequest("POST", "http://here.com/v1/signin", nil)
assert.NoError(t, err)
wl, err := InternalServerError(request, recorder, errors.New("Failed to do something"))
wl, err := WriteStatusAndError(request, recorder, http.StatusForbidden, errors.New("Error one"))
assert.NoError(t, err)
assert.True(t, wl > 0)

msg := "Failed to do something"
//Default type is application/json
assert.Equal(t, http.StatusForbidden, recorder.Result().StatusCode)
assert.Equal(t, "application/json", recorder.Header().Get("Content-Type"))
}

contentType := recorder.Header().Get("content-type")
assert.Equal(t, "application/json", contentType)
func TestSetStatus(t *testing.T) {
testCases := []struct {
cases string
handler func(req *http.Request, resp http.ResponseWriter, errs ...error) (int, error)
statusCode int
}{
{"Forbidden", Forbidden, http.StatusForbidden},
{"BadRequest", BadRequest, http.StatusBadRequest},
{"OK", OK, http.StatusOK},
{"NotFound", NotFound, http.StatusNotFound},
{"Unauthorized", Unauthorized, http.StatusUnauthorized},
{"InternalServerError", InternalServerError, http.StatusInternalServerError},
{"Conflict", Conflict, http.StatusConflict},
{"UnprocessableEntity", UnprocessableEntity, http.StatusUnprocessableEntity},
}

payload := ErrorPayload{}
out := recorder.Body.Bytes()
err = json.Unmarshal(out, &payload)
assert.NoError(t, err)
for _, tc := range testCases {
t.Run(tc.cases, func(t *testing.T) {
recorder := httptest.NewRecorder()

assert.True(t, payload.Error)
assert.Len(t, payload.Message, len(msg))
request, err := http.NewRequest("POST", "http://here.com/v1/signin", nil)
assert.NoError(t, err)
wl, err := tc.handler(request, recorder, errors.New("Failed to do something"))
assert.NoError(t, err)
assert.True(t, wl > 0)
assert.Equal(t, tc.statusCode, recorder.Result().StatusCode)

})
}
}
56 changes: 41 additions & 15 deletions src/net/http/responsetest/assert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,27 @@ func ExampleAssertErrorMessage() {
}

func TestAssertError(t *testing.T) {
recorder := httptest.NewRecorder()
request, err := http.NewRequest("POST", "http://here.com/v1/signin", nil)
assert.NoError(t, err)
testCases := []struct {
cases string
contentType string
}{
{"json", "text/json"},
{"xml", "text/xml"},
}

wl, err := response.InternalServerError(request, recorder, errors.New("Failed to do something"))
assert.NoError(t, err)
assert.True(t, wl > 0)
AssertError(t, recorder)
for _, tc := range testCases {
t.Run(tc.cases, func(t *testing.T) {
recorder := httptest.NewRecorder()
request, err := http.NewRequest("POST", "http://here.com/v1/signin", nil)
assert.NoError(t, err)

request.Header.Set("Content-Type", tc.contentType)
wl, err := response.InternalServerError(request, recorder, errors.New("Failed to do something"))
assert.NoError(t, err)
assert.True(t, wl > 0)
AssertError(t, recorder)
})
}
}

func TestAssertStatusEqual(t *testing.T) {
Expand All @@ -56,13 +69,26 @@ func TestAssertStatusEqual(t *testing.T) {
}

func TestAssertErrorMessage(t *testing.T) {
recorder := httptest.NewRecorder()
request, err := http.NewRequest("POST", "http://here.com/v1/signin", nil)
assert.NoError(t, err)
testCases := []struct {
cases string
contentType string
}{
{"json", "text/json"},
{"xml", "text/xml"},
}

msg := "Failed to do something"
wl, err := response.InternalServerError(request, recorder, errors.New(msg))
assert.NoError(t, err)
assert.True(t, wl > 0)
AssertErrorMessage(t, recorder, msg)
for _, tc := range testCases {
t.Run(tc.cases, func(t *testing.T) {
recorder := httptest.NewRecorder()
request, err := http.NewRequest("POST", "http://here.com/v1/signin", nil)
assert.NoError(t, err)
request.Header.Set("Content-Type", tc.contentType)

msg := "Failed to do something"
wl, err := response.InternalServerError(request, recorder, errors.New(msg))
assert.NoError(t, err)
assert.True(t, wl > 0)
AssertErrorMessage(t, recorder, msg)
})
}
}

0 comments on commit fe8c2d4

Please sign in to comment.