Skip to content

Commit

Permalink
Added test and update Gopkg.lock
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio Andres Virviescas Santana committed Sep 26, 2018
1 parent 499a1fb commit 6f7994a
Show file tree
Hide file tree
Showing 2 changed files with 176 additions and 5 deletions.
4 changes: 2 additions & 2 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

177 changes: 174 additions & 3 deletions response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,63 @@ func TestJSONResponse(t *testing.T) {
}

func TestHTTPResponse(t *testing.T) {
type args struct {
body string
statusCode int
contentType string
}
type want struct {
body string
statusCode int
contentType string
}
tests := []struct {
name string
args args
want want
}{
{
name: "Test",
args: args{
body: "<h1>Test</h1>",
statusCode: 200,
},
want: want{
body: "<h1>Test</h1>",
statusCode: 200,
contentType: "text/html; charset=utf-8",
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := new(fasthttp.RequestCtx)
actx := acquireRequestCtx(ctx)

if err := actx.HTTPResponse(tt.args.body, tt.args.statusCode); err != nil {
t.Errorf("HTTPResponse() error: %v", err)
}

responseBody := string(bytes.TrimSpace(actx.Response.Body()))
if responseBody != tt.want.body {
t.Errorf("body: '%v', want: '%v'", responseBody, tt.want.body)
}

responseStatusCode := actx.Response.StatusCode()
if responseStatusCode != tt.want.statusCode {
t.Errorf("status_code: '%v', want: '%v'", responseStatusCode, tt.want.statusCode)
}

responseContentType := string(actx.Response.Header.ContentType())
if responseContentType != tt.want.contentType {
t.Errorf("content-type: '%v', want: '%v'", responseContentType, tt.want.contentType)
}
})
}
}

func TestHTTPResponseBytes(t *testing.T) {
type args struct {
body []byte
statusCode int
Expand Down Expand Up @@ -159,7 +216,7 @@ func TestHTTPResponse(t *testing.T) {
ctx := new(fasthttp.RequestCtx)
actx := acquireRequestCtx(ctx)

if err := actx.HTTPResponse(tt.args.body, tt.args.statusCode); err != nil {
if err := actx.HTTPResponseBytes(tt.args.body, tt.args.statusCode); err != nil {
t.Errorf("HTTPResponse() error: %v", err)
}

Expand All @@ -182,6 +239,63 @@ func TestHTTPResponse(t *testing.T) {
}

func TestTextResponse(t *testing.T) {
type args struct {
body string
statusCode int
contentType string
}
type want struct {
body string
statusCode int
contentType string
}
tests := []struct {
name string
args args
want want
}{
{
name: "Test",
args: args{
body: "<h1>Test</h1>",
statusCode: 200,
},
want: want{
body: "<h1>Test</h1>",
statusCode: 200,
contentType: "text/plain; charset=utf-8",
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := new(fasthttp.RequestCtx)
actx := acquireRequestCtx(ctx)

if err := actx.TextResponse(tt.args.body, tt.args.statusCode); err != nil {
t.Errorf("TextResponse() error: %v", err)
}

responseBody := string(bytes.TrimSpace(actx.Response.Body()))
if responseBody != tt.want.body {
t.Errorf("body: '%v', want: '%v'", responseBody, tt.want.body)
}

responseStatusCode := actx.Response.StatusCode()
if responseStatusCode != tt.want.statusCode {
t.Errorf("status_code: '%v', want: '%v'", responseStatusCode, tt.want.statusCode)
}

responseContentType := string(actx.Response.Header.ContentType())
if responseContentType != tt.want.contentType {
t.Errorf("content-type: '%v', want: '%v'", responseContentType, tt.want.contentType)
}
})
}
}

func TestTextResponseBytes(t *testing.T) {
type args struct {
body []byte
statusCode int
Expand Down Expand Up @@ -216,7 +330,7 @@ func TestTextResponse(t *testing.T) {
ctx := new(fasthttp.RequestCtx)
actx := acquireRequestCtx(ctx)

if err := actx.TextResponse(tt.args.body, tt.args.statusCode); err != nil {
if err := actx.TextResponseBytes(tt.args.body, tt.args.statusCode); err != nil {
t.Errorf("TextResponse() error: %v", err)
}

Expand All @@ -239,6 +353,63 @@ func TestTextResponse(t *testing.T) {
}

func TestRawResponse(t *testing.T) {
type args struct {
body string
statusCode int
contentType string
}
type want struct {
body string
statusCode int
contentType string
}
tests := []struct {
name string
args args
want want
}{
{
name: "Test",
args: args{
body: "<h1>Test</h1>",
statusCode: 200,
},
want: want{
body: "<h1>Test</h1>",
statusCode: 200,
contentType: "application/octet-stream",
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := new(fasthttp.RequestCtx)
actx := acquireRequestCtx(ctx)

if err := actx.RawResponse(tt.args.body, tt.args.statusCode); err != nil {
t.Errorf("RawResponse() error: %v", err)
}

responseBody := string(bytes.TrimSpace(actx.Response.Body()))
if responseBody != tt.want.body {
t.Errorf("body: '%v', want: '%v'", responseBody, tt.want.body)
}

responseStatusCode := actx.Response.StatusCode()
if responseStatusCode != tt.want.statusCode {
t.Errorf("status_code: '%v', want: '%v'", responseStatusCode, tt.want.statusCode)
}

responseContentType := string(actx.Response.Header.ContentType())
if responseContentType != tt.want.contentType {
t.Errorf("content-type: '%v', want: '%v'", responseContentType, tt.want.contentType)
}
})
}
}

func TestRawResponseBytes(t *testing.T) {
type args struct {
body []byte
statusCode int
Expand Down Expand Up @@ -273,7 +444,7 @@ func TestRawResponse(t *testing.T) {
ctx := new(fasthttp.RequestCtx)
actx := acquireRequestCtx(ctx)

if err := actx.RawResponse(tt.args.body, tt.args.statusCode); err != nil {
if err := actx.RawResponseBytes(tt.args.body, tt.args.statusCode); err != nil {
t.Errorf("RawResponse() error: %v", err)
}

Expand Down

0 comments on commit 6f7994a

Please sign in to comment.