-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
70 lines (67 loc) · 2.11 KB
/
Copy pathmain_test.go
File metadata and controls
70 lines (67 loc) · 2.11 KB
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
package main
import (
"bytes"
"io"
"net/http"
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestSim(t *testing.T) {
os.RemoveAll(os.Getenv("HOME") + "/.kitproj/sim/db")
os.Args = []string{"sim", "examples"}
go main()
time.Sleep(time.Second)
t.Run("hello", func(t *testing.T) {
resp, err := http.Get("http://localhost:8080/hello")
assert.NoError(t, err)
assert.Equal(t, 200, resp.StatusCode)
})
t.Run("script", func(t *testing.T) {
resp, err := http.Get("http://localhost:4040/teapot")
assert.NoError(t, err)
assert.Equal(t, 418, resp.StatusCode)
assert.Equal(t, "true", resp.Header.Get("Teapot"))
data, err := io.ReadAll(resp.Body)
assert.NoError(t, err)
assert.Equal(t, "{\"message\":\"I'm a teapot\"}\n", string(data))
})
t.Run("state", func(t *testing.T) {
t.Run("listDocuments", func(t *testing.T) {
resp, err := http.Get("http://localhost:4040/documents")
assert.NoError(t, err)
assert.Equal(t, 200, resp.StatusCode)
data, err := io.ReadAll(resp.Body)
assert.NoError(t, err)
assert.Equal(t, "[]\n", string(data))
})
var location string
t.Run("createDocument", func(t *testing.T) {
resp, err := http.Post("http://localhost:4040/documents", "", bytes.NewBufferString("{\"foo\": \"bar\"}"))
assert.NoError(t, err)
assert.Equal(t, 201, resp.StatusCode)
location = resp.Header.Get("Location")
})
t.Run("getDocument", func(t *testing.T) {
resp, err := http.Get("http://localhost:4040" + location)
assert.NoError(t, err)
assert.Equal(t, 200, resp.StatusCode)
data, err := io.ReadAll(resp.Body)
assert.NoError(t, err)
assert.Equal(t, "{\"foo\":\"bar\"}\n", string(data))
})
t.Run("deleteDocument", func(t *testing.T) {
req, err := http.NewRequest(http.MethodDelete, "http://localhost:4040"+location, nil)
assert.NoError(t, err)
resp, err := http.DefaultClient.Do(req)
assert.Equal(t, 204, resp.StatusCode)
})
})
t.Run("proxy", func(t *testing.T) {
resp, err := http.Get("http://localhost:5050/proxy")
assert.NoError(t, err)
assert.Equal(t, 200, resp.StatusCode)
assert.Equal(t, "true", resp.Header.Get("Proxy"))
})
}