Skip to content
This repository has been archived by the owner on Apr 11, 2020. It is now read-only.

Commit

Permalink
chore: update test
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed May 18, 2019
1 parent 6053262 commit 09c057e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 28 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module github.com/vicanso/cod-proxy

require (
github.com/stretchr/testify v1.3.0
github.com/vicanso/cod v0.1.1
github.com/vicanso/hes v0.1.4
)
46 changes: 18 additions & 28 deletions proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import (
"net/url"
"testing"

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

func TestProxy(t *testing.T) {
t.Run("normal", func(t *testing.T) {
assert := assert.New(t)
target, _ := url.Parse("https://github.com")
config := Config{
Target: target,
Expand All @@ -34,22 +36,15 @@ func TestProxy(t *testing.T) {
return nil
}
fn(c)
if c.GetHeader("Content-Encoding") != "gzip" {
t.Fatalf("should return gzip data")
}
if c.Request.URL.Path != originalPath {
t.Fatalf("request path should be reverted")
}
if req.Host != originalHost {
t.Fatalf("request host should be reverted")
}

if !done || c.StatusCode != http.StatusOK {
t.Fatalf("http proxy fail")
}
assert.Equal(c.GetHeader("Content-Encoding"), "gzip")
assert.Equal(c.Request.URL.Path, originalPath)
assert.Equal(req.Host, originalHost)
assert.True(done)
assert.Equal(c.StatusCode, http.StatusOK)
})

t.Run("target picker", func(t *testing.T) {
assert := assert.New(t)
target, _ := url.Parse("https://www.baidu.com")
config := Config{
TargetPicker: func(c *cod.Context) (*url.URL, error) {
Expand All @@ -68,12 +63,12 @@ func TestProxy(t *testing.T) {
return nil
}
fn(c)
if !done || c.StatusCode != http.StatusOK {
t.Fatalf("http proxy fail")
}
assert.True(done)
assert.Equal(c.StatusCode, http.StatusOK)
})

t.Run("target picker error", func(t *testing.T) {
assert := assert.New(t)
config := Config{
TargetPicker: func(c *cod.Context) (*url.URL, error) {
return nil, errors.New("abcd")
Expand All @@ -86,12 +81,11 @@ func TestProxy(t *testing.T) {
resp := httptest.NewRecorder()
c := cod.NewContext(resp, req)
err := fn(c)
if err.Error() != "abcd" {
t.Fatalf("proxy should return error")
}
assert.Equal(err.Error(), "abcd")
})

t.Run("no target", func(t *testing.T) {
assert := assert.New(t)
config := Config{
TargetPicker: func(c *cod.Context) (*url.URL, error) {
return nil, nil
Expand All @@ -104,12 +98,11 @@ func TestProxy(t *testing.T) {
resp := httptest.NewRecorder()
c := cod.NewContext(resp, req)
err := fn(c)
if err.Error() != "category=cod-proxy, message=target can not be nil" {
t.Fatalf("nil proxy should return error")
}
assert.Equal(err.Error(), "category=cod-proxy, message=target can not be nil")
})

t.Run("proxy error", func(t *testing.T) {
assert := assert.New(t)
target, _ := url.Parse("https://a")
config := Config{
TargetPicker: func(c *cod.Context) (*url.URL, error) {
Expand All @@ -125,12 +118,11 @@ func TestProxy(t *testing.T) {
return nil
}
err := fn(c)
if err == nil {
t.Fatalf("catch proxy error fail")
}
assert.NotNil(err)
})

t.Run("proxy done", func(t *testing.T) {
assert := assert.New(t)
target, _ := url.Parse("https://www.baidu.com")
done := false
config := Config{
Expand All @@ -149,8 +141,6 @@ func TestProxy(t *testing.T) {
return nil
}
fn(c)
if !done {
t.Fatalf("done callback function should be called")
}
assert.True(done)
})
}

0 comments on commit 09c057e

Please sign in to comment.