|
1 | 1 | package apps |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "encoding/json" |
5 | 4 | "fmt" |
6 | 5 | "io" |
7 | 6 | "log" |
8 | 7 | "net/http" |
9 | 8 | "net/http/httptest" |
10 | 9 | "reflect" |
| 10 | + "sync" |
11 | 11 | "testing" |
12 | 12 |
|
13 | 13 | drycc "github.com/drycc/controller-sdk-go" |
14 | 14 | "github.com/drycc/controller-sdk-go/api" |
15 | | - "github.com/gorilla/websocket" |
| 15 | + "golang.org/x/net/websocket" |
16 | 16 | ) |
17 | 17 |
|
18 | 18 | const appFixture string = ` |
@@ -46,19 +46,11 @@ const appCreateExpected string = `{"id":"example-go"}` |
46 | 46 | const appRunExpected string = `{"command":"echo hi"}` |
47 | 47 | const appTransferExpected string = `{"owner":"test"}` |
48 | 48 |
|
49 | | -var upgrader = websocket.Upgrader{} // use default options |
50 | | - |
51 | 49 | type fakeHTTPServer struct { |
52 | 50 | createID bool |
53 | 51 | createWithoutID bool |
54 | 52 | } |
55 | 53 |
|
56 | | -type fakeLogReqMessage struct { |
57 | | - Lines int `json:"lines"` |
58 | | - Timeout int `json:"timeout"` |
59 | | - Follow bool `json:"follow"` |
60 | | -} |
61 | | - |
62 | 54 | func (f *fakeHTTPServer) ServeHTTP(res http.ResponseWriter, req *http.Request) { |
63 | 55 | res.Header().Add("DRYCC_API_VERSION", drycc.APIVersion) |
64 | 56 |
|
@@ -105,28 +97,6 @@ func (f *fakeHTTPServer) ServeHTTP(res http.ResponseWriter, req *http.Request) { |
105 | 97 | return |
106 | 98 | } |
107 | 99 |
|
108 | | - // The entire log message is prefixed and suffixed with a few characters (not entirely sure why) |
109 | | - // We mimic those here |
110 | | - if req.URL.Path == "/v2/apps/example-go/logs" { |
111 | | - c, err := upgrader.Upgrade(res, req, nil) |
112 | | - if err != nil { |
113 | | - log.Print("upgrade:", err) |
114 | | - return |
115 | | - } |
116 | | - defer c.Close() |
117 | | - _, message, err := c.ReadMessage() |
118 | | - if err != nil { |
119 | | - log.Print("upgrade:", err) |
120 | | - return |
121 | | - } |
122 | | - reqJSON := fakeLogReqMessage{} |
123 | | - json.Unmarshal([]byte(message), &reqJSON) |
124 | | - for i := 0; i < reqJSON.Lines; i++ { |
125 | | - c.WriteMessage(websocket.TextMessage, []byte(fmt.Sprintf("test %d", i))) |
126 | | - } |
127 | | - return |
128 | | - } |
129 | | - |
130 | 100 | if req.URL.Path == "/v2/apps/example-go/run" && req.Method == "POST" { |
131 | 101 | body, err := io.ReadAll(req.Body) |
132 | 102 |
|
@@ -317,26 +287,39 @@ func TestAppsList(t *testing.T) { |
317 | 287 |
|
318 | 288 | func TestAppsLogs(t *testing.T) { |
319 | 289 | t.Parallel() |
320 | | - |
321 | | - handler := fakeHTTPServer{} |
322 | | - server := httptest.NewServer(&handler) |
323 | | - defer server.Close() |
324 | | - |
325 | | - drycc, err := drycc.New(false, server.URL, "abc") |
| 290 | + var once sync.Once |
| 291 | + var addr string |
| 292 | + once.Do(func() { |
| 293 | + http.Handle( |
| 294 | + "/v2/apps/example-go/logs", |
| 295 | + websocket.Handler(func(conn *websocket.Conn) { |
| 296 | + io.Copy(conn, conn) |
| 297 | + }), |
| 298 | + ) |
| 299 | + server := httptest.NewServer(nil) |
| 300 | + addr = server.Listener.Addr().String() |
| 301 | + log.Print("Test WebSocket server listening on ", addr) |
| 302 | + }) |
| 303 | + drycc, err := drycc.New(false, addr, "abc") |
326 | 304 | if err != nil { |
327 | 305 | t.Fatal(err) |
328 | 306 | } |
329 | | - |
330 | | - conn, err := Logs(drycc, "example-go", 1, false, 300) |
| 307 | + expected := api.AppLogsRequest{ |
| 308 | + Lines: 1, |
| 309 | + Follow: false, |
| 310 | + Timeout: 300, |
| 311 | + } |
| 312 | + conn, err := Logs(drycc, "example-go", expected) |
331 | 313 | if err != nil { |
332 | 314 | t.Fatal(err) |
333 | 315 | } |
334 | | - _, message, err := conn.ReadMessage() |
| 316 | + var actual api.AppLogsRequest |
| 317 | + err = websocket.JSON.Receive(conn, &actual) |
335 | 318 | if err != nil { |
336 | 319 | t.Fatal(err) |
337 | 320 | } |
338 | | - if string(message) != "test 0" { |
339 | | - t.Errorf("Expected %s, Got %s", "test 0", message) |
| 321 | + if !reflect.DeepEqual(actual, expected) { |
| 322 | + t.Errorf("Expected: %v, Got %v", expected, actual) |
340 | 323 | } |
341 | 324 | } |
342 | 325 |
|
|
0 commit comments