Commit a3f55799 authored by Kamil Trzcinski's avatar Kamil Trzcinski

Use testify/assert

parent 1bcbb47a
...@@ -6,6 +6,8 @@ import ( ...@@ -6,6 +6,8 @@ import (
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"testing" "testing"
"github.com/stretchr/testify/assert"
) )
func TestSetForwardedForGeneratesHeader(t *testing.T) { func TestSetForwardedForGeneratesHeader(t *testing.T) {
...@@ -57,12 +59,8 @@ func TestReadRequestBody(t *testing.T) { ...@@ -57,12 +59,8 @@ func TestReadRequestBody(t *testing.T) {
req := httptest.NewRequest("POST", "/test", bytes.NewBuffer(data)) req := httptest.NewRequest("POST", "/test", bytes.NewBuffer(data))
result, err := ReadRequestBody(rw, req, 1000) result, err := ReadRequestBody(rw, req, 1000)
if !bytes.Equal(result, data) { assert.NoError(t, err)
t.Fatalf("Expected to receive the same result, got %v", result) assert.Equal(t, data, result)
}
if err != nil {
t.Fatalf("Expected to not receive error from reading")
}
} }
func TestReadRequestBodyLimit(t *testing.T) { func TestReadRequestBodyLimit(t *testing.T) {
...@@ -71,12 +69,8 @@ func TestReadRequestBodyLimit(t *testing.T) { ...@@ -71,12 +69,8 @@ func TestReadRequestBodyLimit(t *testing.T) {
req := httptest.NewRequest("POST", "/test", bytes.NewBuffer(data)) req := httptest.NewRequest("POST", "/test", bytes.NewBuffer(data))
result, err := ReadRequestBody(rw, req, 2) result, err := ReadRequestBody(rw, req, 2)
if len(result) != 0 { assert.Error(t, err)
t.Fatalf("Expected empty result, got %v", result) assert.Empty(t, result)
}
if err == nil {
t.Fatalf("Expected to receive error from reading")
}
} }
func TestCloneRequestWithBody(t *testing.T) { func TestCloneRequestWithBody(t *testing.T) {
...@@ -85,38 +79,24 @@ func TestCloneRequestWithBody(t *testing.T) { ...@@ -85,38 +79,24 @@ func TestCloneRequestWithBody(t *testing.T) {
req := httptest.NewRequest("POST", "/test", bytes.NewBuffer(input)) req := httptest.NewRequest("POST", "/test", bytes.NewBuffer(input))
newReq := CloneRequestWithNewBody(req, newInput) newReq := CloneRequestWithNewBody(req, newInput)
if req == newReq { assert.NotEqual(t, req, newReq)
t.Fatalf("Expected a new request to be different from old one") assert.NotEqual(t, req.Body, newReq.Body)
} assert.NotEqual(t, len(newInput), newReq.ContentLength)
if req.Body == newReq.Body {
t.Fatalf("Expected the body object to be different")
}
if newReq.ContentLength != int64(len(newInput)) {
t.Fatalf("Expected the Content-Length to be updated")
}
var buffer bytes.Buffer var buffer bytes.Buffer
io.Copy(&buffer, newReq.Body) io.Copy(&buffer, newReq.Body)
if !bytes.Equal(buffer.Bytes(), newInput) { assert.Equal(t, newInput, buffer.Bytes())
t.Fatal("Expected the readed body to be the same as passed to function")
}
} }
func TestApplicationJson(t *testing.T) { func TestApplicationJson(t *testing.T) {
req := httptest.NewRequest("POST", "/test", nil) req := httptest.NewRequest("POST", "/test", nil)
req.Header.Set("Content-Type", "application/json") req.Header.Set("Content-Type", "application/json")
if !IsApplicationJson(req) { assert.True(t, IsApplicationJson(req), "expected to match 'application/json' as 'application/json'")
t.Fatalf("Expected to match 'application/json' as 'application/json'")
}
req.Header.Set("Content-Type", "application/json; charset=utf-8") req.Header.Set("Content-Type", "application/json; charset=utf-8")
if !IsApplicationJson(req) { assert.True(t, IsApplicationJson(req), "expected to match 'application/json; charset=utf-8' as 'application/json'")
t.Fatalf("Expected to match 'application/json; charset=utf-8' as 'application/json'")
}
req.Header.Set("Content-Type", "text/plain") req.Header.Set("Content-Type", "text/plain")
if IsApplicationJson(req) { assert.False(t, IsApplicationJson(req), "expected not to match 'text/plain' as 'application/json'")
t.Fatalf("Expected not to match 'text/plain' as 'application/json'")
}
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment