This repository has been archived by the owner on Dec 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 74
/
main_test.go
140 lines (117 loc) · 3.32 KB
/
main_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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package main
import (
"encoding/json"
. "github.com/aandryashin/matchers"
. "github.com/aandryashin/matchers/httpresp"
"github.com/aerokube/util/sse"
"io"
"net/http"
"net/http/httptest"
"net/url"
"testing"
)
var (
srv *httptest.Server
)
var _ = func() bool {
testing.Init()
return true
}()
func init() {
broker := sse.NewSseBroker()
srv = httptest.NewServer(mux(broker))
gitRevision = "test-revision"
}
func selenoidApi() http.Handler {
mux := http.NewServeMux()
mux.HandleFunc("/status", mockStatus)
return mux
}
func mockStatus(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{
"total": 5,
"used": 0,
"queued": 0,
"pending": 0,
"browsers": {
"firefox": {
"61.0": {
}
}
},
"videos":["test_chrome.mp4"]
}`))
}
func videoApi() http.Handler {
mux := http.NewServeMux()
mux.HandleFunc("/video/", mockVideo)
return mux
}
func mockVideo(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("im a video"))
}
func withUrl(path string) string {
return srv.URL + path
}
func TestRootLoads(t *testing.T) {
resp, err := http.Get(withUrl("/"))
AssertThat(t, err, Is{nil})
AssertThat(t, resp, Code{200})
}
func TestPing(t *testing.T) {
rsp, err := http.Get(withUrl("/ping"))
AssertThat(t, err, Is{nil})
AssertThat(t, rsp, Code{http.StatusOK})
AssertThat(t, rsp.Body, Is{Not{nil}})
var data map[string]interface{}
bt, readErr := io.ReadAll(rsp.Body)
AssertThat(t, readErr, Is{nil})
jsonErr := json.Unmarshal(bt, &data)
AssertThat(t, jsonErr, Is{nil})
_, hasUptime := data["uptime"]
AssertThat(t, hasUptime, Is{true})
version, hasVersion := data["version"]
AssertThat(t, hasVersion, Is{true})
AssertThat(t, version, EqualTo{"test-revision"})
}
func TestStatus(t *testing.T) {
selenoid := httptest.NewServer(selenoidApi())
statusURI, _ = url.Parse(selenoid.URL)
rsp, err := http.Get(withUrl("/status"))
AssertThat(t, err, Is{nil})
AssertThat(t, rsp, Code{http.StatusOK})
AssertThat(t, rsp.Body, Is{Not{nil}})
AssertThat(t, rsp.Header.Get("Content-Type"), Is{"application/json"})
}
func TestVideo(t *testing.T) {
video := httptest.NewServer(videoApi())
statusURI, _ = url.Parse(video.URL)
rsp, err := http.Get(withUrl("/video/test_chrome.mp4"))
AssertThat(t, err, Is{nil})
AssertThat(t, rsp, Code{http.StatusOK})
AssertThat(t, rsp.Body, Is{Not{nil}})
}
func TestVideoFail(t *testing.T) {
statusURI, _ = url.Parse("http://localhost:1")
rsp, err := http.Get(withUrl("/video/test_chrome1.mp4"))
AssertThat(t, err, Is{nil})
AssertThat(t, rsp, Code{http.StatusBadGateway})
AssertThat(t, rsp.Body, Not{Is{nil}})
}
func TestStatusError(t *testing.T) {
statusURI, _ = url.Parse("http://localhost:1")
rsp, err := http.Get(withUrl("/status"))
AssertThat(t, err, Is{nil})
AssertThat(t, rsp, Code{http.StatusInternalServerError})
AssertThat(t, rsp.Body, Is{Not{nil}})
AssertThat(t, rsp.Header.Get("Content-Type"), Is{"application/json"})
}
func TestCheckOrigin(t *testing.T) {
r, _ := http.NewRequest(http.MethodGet, "http://localhost", nil)
r.Header.Add("Origin", "some-host.example.com")
AssertThat(t, checkOrigin("*")(r), Is{true})
AssertThat(t, checkOrigin("some-host.example.com,another-host.example.com")(r), Is{true})
AssertThat(t, checkOrigin("missing-host.example.com,another-host.example.com")(r), Is{false})
}