Test_echoServer tests the echoServer by sending it 5 different messages and ensuring the responses all match.
(t *testing.T)
| 13 | // Test_echoServer tests the echoServer by sending it 5 different messages |
| 14 | // and ensuring the responses all match. |
| 15 | func Test_echoServer(t *testing.T) { |
| 16 | t.Parallel() |
| 17 | |
| 18 | s := httptest.NewServer(echoServer{ |
| 19 | logf: t.Logf, |
| 20 | }) |
| 21 | defer s.Close() |
| 22 | |
| 23 | ctx, cancel := context.WithTimeout(context.Background(), time.Minute) |
| 24 | defer cancel() |
| 25 | |
| 26 | c, _, err := websocket.Dial(ctx, s.URL, &websocket.DialOptions{ |
| 27 | Subprotocols: []string{"echo"}, |
| 28 | }) |
| 29 | if err != nil { |
| 30 | t.Fatal(err) |
| 31 | } |
| 32 | defer c.Close(websocket.StatusInternalError, "the sky is falling") |
| 33 | |
| 34 | for i := 0; i < 5; i++ { |
| 35 | err = wsjson.Write(ctx, c, map[string]int{ |
| 36 | "i": i, |
| 37 | }) |
| 38 | if err != nil { |
| 39 | t.Fatal(err) |
| 40 | } |
| 41 | |
| 42 | v := map[string]int{} |
| 43 | err = wsjson.Read(ctx, c, &v) |
| 44 | if err != nil { |
| 45 | t.Fatal(err) |
| 46 | } |
| 47 | |
| 48 | if v["i"] != i { |
| 49 | t.Fatalf("expected %v but got %v", i, v) |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | c.Close(websocket.StatusNormalClosure, "") |
| 54 | } |