This repository has been archived by the owner on Oct 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmoon_test.go
119 lines (100 loc) · 3.13 KB
/
moon_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package moon
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/goji/httpauth"
"golang.org/x/net/context"
)
func assertEquals(t *testing.T, e interface{}, o interface{}) {
if e != o {
t.Errorf("\n...expected = %v\n...obtained = %v", e, o)
}
}
func assertHasPrefix(t *testing.T, o interface{}, prefix string) {
if strings.HasPrefix(o.(string), prefix) == false {
t.Errorf("\n...expected prefix = %v\n...obtained = %v", prefix, o)
}
}
func serveAndRequest(h http.Handler, auth bool) string {
//auth := base64.StdEncoding.EncodeToString([]byte("user:pass"))
//r.Header.Set("Authorization", "Basic "+auth)
ts := httptest.NewServer(h)
defer ts.Close()
req, _ := http.NewRequest("GET", ts.URL, nil)
if auth {
req.SetBasicAuth("user", "pass")
}
res, err := http.DefaultClient.Do(req)
//res, err := http.Client.Do(req)
// res, err := http.Get(ts.URL)
if err != nil {
log.Fatal(err)
}
resBody, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
log.Fatal(err)
}
// log.Print(resBody)
return string(resBody)
}
func tokenMiddlewareA(ctx context.Context, next Next) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx = context.WithValue(ctx, "tokenA", "123")
next(ctx)
})
}
func tokenMiddlewareB(ctx context.Context, next Next) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx = context.WithValue(ctx, "tokenB", "456")
next(ctx)
})
}
func tokenHandler(ctx context.Context) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Tokens are: %v, %v", ctx.Value("tokenA"), ctx.Value("tokenB"))
})
}
func TestContext(t *testing.T) {
st := New(tokenMiddlewareA, tokenMiddlewareB).Then(tokenHandler)
res := serveAndRequest(st, false)
assertEquals(t, "Tokens are: 123, 456", res)
}
func TestContextRoot(t *testing.T) {
Context = func(r *http.Request) context.Context {
ctx := context.TODO()
return context.WithValue(ctx, "tokenA", "789")
}
defer func() { Context = nil }()
mws := New(tokenMiddlewareB).Then(tokenHandler)
res := serveAndRequest(mws, false)
assertEquals(t, "Tokens are: 789, 456", res)
}
func authHandler(ctx context.Context) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
u, p, _ := r.BasicAuth()
fmt.Fprintf(w, "Hello: %v, %v", u, p)
})
}
func TestGojiBasicAuthUnauthorized(t *testing.T) {
mws := New(Adapt(httpauth.SimpleBasicAuth("user", "pass"))).Then(authHandler)
res := serveAndRequest(mws, false)
assertEquals(t, "Unauthorized\n", res)
}
func TestGojiBasicAuthAuthorized(t *testing.T) {
mws := New(Adapt(httpauth.SimpleBasicAuth("user", "pass"))).Then(authHandler)
res := serveAndRequest(mws, true)
assertEquals(t, "Hello: user, pass", res)
}
func TestContextWithFunc(t *testing.T) {
mws := New(tokenMiddlewareA, tokenMiddlewareB).ThenFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Tokens are: %v, %v", ctx.Value("tokenA"), ctx.Value("tokenB"))
})
res := serveAndRequest(mws, false)
assertEquals(t, "Tokens are: 123, 456", res)
}