-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathweb_test.go
61 lines (53 loc) · 1.49 KB
/
web_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
package beyond
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/drewolson/testflight"
"github.com/stretchr/testify/assert"
)
var (
echoServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Println(err)
}
switch string(b) {
case "ping":
fmt.Fprint(w, "pong")
default:
fmt.Fprint(w, string(b))
}
}))
testMux http.Handler
)
func init() {
testMux = NewMux()
}
func TestWebPOST(t *testing.T) {
testflight.WithServer(testMux, func(rq *testflight.Requester) {
request, err := http.NewRequest("POST", "/", strings.NewReader("ping"))
request.Host = echoServer.URL[7:] // strip the http://
assert.NoError(t, err)
request.SetBasicAuth("", tokenTestUserTokens["user1"])
response := rq.Do(request)
assert.Equal(t, 200, response.StatusCode)
assert.Equal(t, "pong", response.Body)
request, err = http.NewRequest("POST", "/", strings.NewReader("aliens"))
request.Host = echoServer.URL // including http://
assert.NoError(t, err)
request.SetBasicAuth("", tokenTestUserTokens["user1"])
response = rq.Do(request)
assert.Equal(t, 502, response.StatusCode)
assert.Equal(t, "", response.Body)
request, err = http.NewRequest("POST", "/", strings.NewReader("aliens"))
request.Host = echoServer.URL[7:] // strip the http://
assert.NoError(t, err)
response = rq.Do(request)
assert.Equal(t, *fouroOneCode, response.StatusCode)
})
}