forked from quii/mockingjay-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathacceptance_test.go
111 lines (87 loc) · 2.63 KB
/
acceptance_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
package main
import (
"bytes"
"github.com/stretchr/testify/assert"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
var (
app *application
mjServer http.Handler
)
const testYAMLPath = "examples/example.yaml"
func init() {
app = defaultApplication(log.New(ioutil.Discard, "", 0), defaultHTTPTimeoutSeconds)
svr, err := app.CreateServer(testYAMLPath, "", false)
if err != nil {
log.Fatal("Couldn't load MJ config from", testYAMLPath)
}
mjServer = svr
}
func TestIssue42(t *testing.T) {
failApp := defaultApplication(log.New(ioutil.Discard, "", 0), defaultHTTPTimeoutSeconds)
failSvr, _ := failApp.CreateServer("examples/issue42.yaml", "", false)
svr := httptest.NewServer(failSvr)
defer svr.Close()
reqBody := []byte(`{"query":{"match_all":{}}}`)
req, _ := http.NewRequest("POST", svr.URL+"/profile/validate-query", bytes.NewBuffer(reqBody))
req.Header["Content-Type"] = []string{"application/json"}
req.Header["Content-Butt"] = []string{"application/json"}
client := http.Client{}
res, _ := client.Do(req)
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
if res.StatusCode != http.StatusOK {
t.Error("WTF", res, string(body))
}
}
func TestItLaunchesServersAndIsCompatibleWithItsOwnConfig(t *testing.T) {
svr := httptest.NewServer(mjServer)
defer svr.Close()
cdcErrors := app.CheckCompatibility(testYAMLPath, svr.URL)
assert.Empty(t, cdcErrors, "There should be no CDC errors with itself")
}
func TestItListsRequestsItHasReceived(t *testing.T) {
svr := httptest.NewServer(mjServer)
defer svr.Close()
http.Get(svr.URL + "/hello")
res, err := http.Get(svr.URL + "/requests")
assert.Nil(t, err)
assert.Equal(t, res.StatusCode, http.StatusOK)
}
func TestANewEndpointCanBeAdded(t *testing.T) {
svr := httptest.NewServer(mjServer)
defer svr.Close()
newEndpointJSON := `
{
"Name": "Test endpoint",
"CDCDisabled": false,
"Request": {
"URI": "/new-endpoint",
"Method": "GET",
"Headers": null,
"Body": ""
},
"Response": {
"Code": 200,
"Body": "{\"message\": \"hello, world\"}",
"Headers": {
"content-type": "text\/json"
}
}
}
`
newEndpointURL := svr.URL + "/mj-new-endpoint"
res, err := http.Post(newEndpointURL, "application/json", strings.NewReader(newEndpointJSON))
assert.Nil(t, err)
assert.Equal(t, res.StatusCode, http.StatusCreated)
newEndPointResponse, err := http.Get(svr.URL + `/new-endpoint`)
assert.Nil(t, err)
assert.Equal(t, newEndPointResponse.StatusCode, http.StatusOK)
newEndpointBody, _ := ioutil.ReadAll(newEndPointResponse.Body)
assert.Equal(t, string(newEndpointBody), `{"message": "hello, world"}`)
}