This repository has been archived by the owner on Jul 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathapi_test.go
116 lines (93 loc) · 2.65 KB
/
api_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
package apidemic
import (
"bytes"
"encoding/json"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/gorilla/mux"
"github.com/pmylund/go-cache"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestDynamicEndpointFailsWithoutRegistration(t *testing.T) {
s := setUp()
payload := registerPayload(t, "fixtures/sample_request.json")
w := httptest.NewRecorder()
req := jsonRequest("POST", "/api/test", payload)
s.ServeHTTP(w, req)
assert.Equal(t, http.StatusNotFound, w.Code)
}
func TestDynamicEndpointWithGetRequest(t *testing.T) {
s := setUp()
payload := registerPayload(t, "fixtures/sample_request.json")
w := httptest.NewRecorder()
req := jsonRequest("POST", "/register", payload)
s.ServeHTTP(w, req)
require.Equal(t, http.StatusOK, w.Code)
w = httptest.NewRecorder()
req = jsonRequest("GET", "/api/test", nil)
s.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code)
}
func TestDynamicEndpointWithPostRequest(t *testing.T) {
s := setUp()
payload := registerPayload(t, "fixtures/sample_request.json")
payload["http_method"] = "POST"
w := httptest.NewRecorder()
req := jsonRequest("POST", "/register", payload)
s.ServeHTTP(w, req)
require.Equal(t, http.StatusOK, w.Code)
w = httptest.NewRecorder()
req = jsonRequest("POST", "/api/test", nil)
s.ServeHTTP(w, req)
assert.Equal(t, http.StatusCreated, w.Code)
}
func TestDynamicEndpointWithForbiddenResponse(t *testing.T) {
s := setUp()
registerPayload := registerPayload(t, "fixtures/sample_request.json")
registerPayload["response_code_probabilities"] = map[string]int{"403": 100}
w := httptest.NewRecorder()
req := jsonRequest("POST", "/register", registerPayload)
s.ServeHTTP(w, req)
require.Equal(t, http.StatusOK, w.Code)
w = httptest.NewRecorder()
req = jsonRequest("GET", "/api/test", nil)
s.ServeHTTP(w, req)
assert.Equal(t, http.StatusForbidden, w.Code)
}
func setUp() *mux.Router {
store = cache.New(5*time.Minute, 30*time.Second)
return NewServer()
}
func registerPayload(t *testing.T, fixtureFile string) map[string]interface{} {
content, err := ioutil.ReadFile(fixtureFile)
if err != nil {
t.Fatal(err)
}
var api map[string]interface{}
err = json.NewDecoder(bytes.NewReader(content)).Decode(&api)
if err != nil {
t.Fatal(err)
}
return api
}
func jsonRequest(method string, path string, body interface{}) *http.Request {
var bEnd io.Reader
if body != nil {
b, err := json.Marshal(body)
if err != nil {
return nil
}
bEnd = bytes.NewReader(b)
}
req, err := http.NewRequest(method, path, bEnd)
if err != nil {
panic(err)
}
req.Header.Set("Content-Type", "application/json")
return req
}