1
0
mirror of https://github.com/labstack/echo.git synced 2024-12-24 20:14:31 +02:00

Improve test coverage (#544)

* Improve test coverage

* add engine package to coverage profile
This commit is contained in:
Oleg Lobanov 2016-06-06 04:17:01 +05:00 committed by Vishal Rana
parent 09a2ce60a6
commit acc91b28f3
11 changed files with 346 additions and 0 deletions

View File

@ -10,6 +10,8 @@ before_install:
script:
- go test -coverprofile=echo.coverprofile
- go test -coverprofile=middleware.coverprofile ./middleware
- go test -coverprofile=engine_standatd.coverprofile ./engine/standard
- go test -coverprofile=engine_fasthttp.coverprofile ./engine/fasthttp
- $HOME/gopath/bin/gover
- $HOME/gopath/bin/goveralls -coverprofile=gover.coverprofile -service=travis-ci
matrix:

View File

@ -0,0 +1,24 @@
package fasthttp
import (
"github.com/labstack/echo/engine/test"
fast "github.com/valyala/fasthttp"
"testing"
"time"
)
func TestCookie(t *testing.T) {
fCookie := &fast.Cookie{}
fCookie.SetKey("session")
fCookie.SetValue("securetoken")
fCookie.SetPath("/")
fCookie.SetDomain("github.com")
fCookie.SetExpire(time.Date(2016, time.January, 1, 0, 0, 0, 0, time.UTC))
fCookie.SetSecure(true)
fCookie.SetHTTPOnly(true)
cookie := &Cookie{
fCookie,
}
test.CookieTest(t, cookie)
}

View File

@ -0,0 +1,24 @@
package fasthttp
import (
"github.com/labstack/echo/engine/test"
"github.com/stretchr/testify/assert"
fast "github.com/valyala/fasthttp"
"testing"
)
func TestRequestHeader(t *testing.T) {
header := &RequestHeader{&fast.RequestHeader{}}
test.HeaderTest(t, header)
header.reset(&fast.RequestHeader{})
assert.Len(t, header.Keys(), 0)
}
func TestResponseHeader(t *testing.T) {
header := &ResponseHeader{&fast.ResponseHeader{}}
test.HeaderTest(t, header)
header.reset(&fast.ResponseHeader{})
assert.Len(t, header.Keys(), 1)
}

View File

@ -0,0 +1,18 @@
package fasthttp
import (
"github.com/labstack/echo/engine/test"
"github.com/stretchr/testify/assert"
fast "github.com/valyala/fasthttp"
"testing"
)
func TestURL(t *testing.T) {
uri := &fast.URI{}
uri.Parse([]byte("github.com"), []byte("/labstack/echo?param1=value1&param1=value2&param2=value3"))
mUrl := &URL{uri}
test.URLTest(t, mUrl)
mUrl.reset(&fast.URI{})
assert.Equal(t, "", string(mUrl.Host()))
}

View File

@ -0,0 +1,21 @@
package standard
import (
"github.com/labstack/echo/engine/test"
"net/http"
"testing"
"time"
)
func TestCookie(t *testing.T) {
cookie := &Cookie{&http.Cookie{
Name: "session",
Value: "securetoken",
Path: "/",
Domain: "github.com",
Expires: time.Date(2016, time.January, 1, 0, 0, 0, 0, time.UTC),
Secure: true,
HttpOnly: true,
}}
test.CookieTest(t, cookie)
}

View File

@ -0,0 +1,16 @@
package standard
import (
"github.com/labstack/echo/engine/test"
"github.com/stretchr/testify/assert"
"net/http"
"testing"
)
func TestHeader(t *testing.T) {
header := &Header{http.Header{}}
test.HeaderTest(t, header)
header.reset(http.Header{})
assert.Len(t, header.Keys(), 0)
}

View File

@ -0,0 +1,25 @@
package standard
import (
"bufio"
"github.com/labstack/echo/engine/test"
"github.com/labstack/gommon/log"
"github.com/stretchr/testify/assert"
"net/http"
"net/url"
"strings"
"testing"
)
func TestRequest(t *testing.T) {
httpReq, _ := http.ReadRequest(bufio.NewReader(strings.NewReader(test.MultipartRequest)))
url, _ := url.Parse("https://github.com/labstack/echo")
httpReq.URL = url
httpReq.RemoteAddr = "127.0.0.1"
req := NewRequest(httpReq, log.New("echo"))
test.RequestTest(t, req)
nr, _ := http.NewRequest("GET", "/", nil)
req.reset(nr, nil, nil)
assert.Equal(t, "", req.Host())
}

View File

@ -0,0 +1,50 @@
package standard
import (
"github.com/labstack/gommon/log"
"github.com/stretchr/testify/assert"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"time"
)
func TestResponse_WriteHeader(t *testing.T) {
recorder := httptest.NewRecorder()
resp := NewResponse(recorder, log.New("echo"))
resp.WriteHeader(http.StatusNotFound)
assert.Equal(t, http.StatusNotFound, resp.Status())
assert.True(t, resp.Committed())
}
func TestResponse_Write(t *testing.T) {
recorder := httptest.NewRecorder()
resp := NewResponse(recorder, log.New("echo"))
resp.Write([]byte("Hello"))
assert.Equal(t, int64(5), resp.Size())
if body, err := ioutil.ReadAll(recorder.Body); assert.NoError(t, err) {
assert.Equal(t, "Hello", string(body))
}
resp.Flush()
assert.True(t, recorder.Flushed)
}
func TestResponse_SetCookie(t *testing.T) {
recorder := httptest.NewRecorder()
resp := NewResponse(recorder, log.New("echo"))
resp.SetCookie(&Cookie{&http.Cookie{
Name: "session",
Value: "securetoken",
Path: "/",
Domain: "github.com",
Expires: time.Date(2016, time.January, 1, 0, 0, 0, 0, time.UTC),
Secure: true,
HttpOnly: true,
}})
assert.Equal(t, "session=securetoken; Path=/; Domain=github.com; Expires=Fri, 01 Jan 2016 00:00:00 GMT; HttpOnly; Secure", recorder.Header().Get("Set-Cookie"))
}

View File

@ -0,0 +1,17 @@
package standard
import (
"github.com/labstack/echo/engine/test"
"github.com/stretchr/testify/assert"
"net/url"
"testing"
)
func TestURL(t *testing.T) {
u, _ := url.Parse("https://github.com/labstack/echo?param1=value1&param1=value2&param2=value3")
mUrl := &URL{u, nil}
test.URLTest(t, mUrl)
mUrl.reset(&url.URL{})
assert.Equal(t, "", mUrl.Host)
}

View File

@ -0,0 +1,61 @@
package test
import (
"github.com/labstack/echo/engine"
"github.com/stretchr/testify/assert"
"testing"
"time"
)
func HeaderTest(t *testing.T, header engine.Header) {
h := "X-My-Header"
v := "value"
nv := "new value"
h1 := "X-Another-Header"
header.Add(h, v)
assert.Equal(t, v, header.Get(h))
header.Set(h, nv)
assert.Equal(t, nv, header.Get(h))
assert.True(t, header.Contains(h))
header.Del(h)
assert.False(t, header.Contains(h))
header.Add(h, v)
header.Add(h1, v)
for _, expected := range []string{h, h1} {
found := false
for _, actual := range header.Keys() {
if actual == expected {
found = true
break
}
}
if !found {
t.Errorf("Header %s not found", expected)
}
}
}
func URLTest(t *testing.T, url engine.URL) {
path := "/echo/test"
url.SetPath(path)
assert.Equal(t, path, url.Path())
assert.Equal(t, map[string][]string{"param1": []string{"value1", "value2"}, "param2": []string{"value3"}}, url.QueryParams())
assert.Equal(t, "value1", url.QueryParam("param1"))
assert.Equal(t, "param1=value1&param1=value2&param2=value3", url.QueryString())
}
func CookieTest(t *testing.T, coockie engine.Cookie) {
assert.Equal(t, "github.com", coockie.Domain())
assert.Equal(t, time.Date(2016, time.January, 1, 0, 0, 0, 0, time.UTC), coockie.Expires())
assert.True(t, coockie.HTTPOnly())
assert.True(t, coockie.Secure())
assert.Equal(t, "session", coockie.Name())
assert.Equal(t, "/", coockie.Path())
assert.Equal(t, "securetoken", coockie.Value())
}

View File

@ -0,0 +1,88 @@
package test
import (
"github.com/labstack/echo/engine"
"github.com/stretchr/testify/assert"
"io/ioutil"
"strings"
"testing"
)
const MultipartRequest = `POST /labstack/echo HTTP/1.1
Host: github.com
Connection: close
User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X; de-de) AppleWebKit/523.10.3 (KHTML, like Gecko) Version/3.0.4 Safari/523.10
Content-Type: multipart/form-data; boundary=Asrf456BGe4h
Content-Length: 261
Accept-Encoding: gzip
Accept-Charset: ISO-8859-1,UTF-8;q=0.7,*;q=0.7
Cache-Control: no-cache
Accept-Language: de,en;q=0.7,en-us;q=0.3
Referer: https://github.com/
Cookie: session=securetoken; user=123
X-Real-IP: 127.0.0.1
--Asrf456BGe4h
Content-Disposition: form-data; name="foo"
bar
--Asrf456BGe4h
Content-Disposition: form-data; name="baz"
bat
--Asrf456BGe4h
Content-Disposition: form-data; name="note"; filename="note.txt"
Content-Type: text/plain
Hello world!
--Asrf456BGe4h--
`
func RequestTest(t *testing.T, request engine.Request) {
assert.Equal(t, "github.com", request.Host())
request.SetURI("/labstack/echo?token=54321")
assert.Equal(t, "/labstack/echo?token=54321", request.URI())
assert.Equal(t, "/labstack/echo", request.URL().Path())
assert.Equal(t, "https://github.com/", request.Referer())
assert.Equal(t, "127.0.0.1", request.Header().Get("X-Real-IP"))
assert.Equal(t, "https", request.Scheme())
assert.Equal(t, "Mozilla/5.0 (Macintosh; U; Intel Mac OS X; de-de) AppleWebKit/523.10.3 (KHTML, like Gecko) Version/3.0.4 Safari/523.10", request.UserAgent())
assert.Equal(t, "127.0.0.1", request.RemoteAddress())
assert.Equal(t, "POST", request.Method())
assert.Equal(t, int64(261), request.ContentLength())
assert.Equal(t, "bar", request.FormValue("foo"))
if fHeader, err := request.FormFile("note"); assert.NoError(t, err) {
if file, err := fHeader.Open(); assert.NoError(t, err) {
text, _ := ioutil.ReadAll(file)
assert.Equal(t, "Hello world!", string(text))
}
}
assert.Equal(t, map[string][]string{"baz": []string{"bat"}, "foo": []string{"bar"}}, request.FormParams())
if form, err := request.MultipartForm(); assert.NoError(t, err) {
_, ok := form.File["note"]
assert.True(t, ok)
}
request.SetMethod("PUT")
assert.Equal(t, "PUT", request.Method())
request.SetBody(strings.NewReader("Hello"))
if body, err := ioutil.ReadAll(request.Body()); assert.NoError(t, err) {
assert.Equal(t, "Hello", string(body))
}
if cookie, err := request.Cookie("session"); assert.NoError(t, err) {
assert.Equal(t, "securetoken", cookie.Value())
}
_, err := request.Cookie("foo")
assert.Error(t, err)
assert.Equal(t, 2, len(request.Cookies()))
}