(t *testing.T)
| 57 | } |
| 58 | |
| 59 | func TestWebsocketQuery(t *testing.T) { |
| 60 | w := test.GetWorld(t) |
| 61 | pn := w.NewPermanode(t) |
| 62 | test.MustRunCmd(t, w.Cmd("pk-put", "attr", pn.String(), "tag", "foo")) |
| 63 | |
| 64 | check := func(err error) { |
| 65 | if err != nil { |
| 66 | t.Fatalf("%v", err) |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | const bufSize = 1 << 20 |
| 71 | |
| 72 | dialer := websocket.Dialer{ |
| 73 | ReadBufferSize: bufSize, |
| 74 | WriteBufferSize: bufSize, |
| 75 | } |
| 76 | |
| 77 | searchURL := (&url.URL{Scheme: "ws", Host: w.Addr(), Path: w.SearchHandlerPath() + "ws"}).String() |
| 78 | wsHeaders := http.Header{ |
| 79 | "Origin": {"http://" + w.Addr()}, |
| 80 | } |
| 81 | |
| 82 | wc, _, err := dialer.Dial(searchURL, wsHeaders) |
| 83 | check(err) |
| 84 | |
| 85 | msg, err := wc.NextWriter(websocket.TextMessage) |
| 86 | check(err) |
| 87 | |
| 88 | _, err = msg.Write([]byte(`{"tag": "foo", "query": { "expression": "tag:foo" }}`)) |
| 89 | check(err) |
| 90 | check(msg.Close()) |
| 91 | |
| 92 | errc := make(chan error, 1) |
| 93 | go func() { |
| 94 | inType, inMsg, err := wc.ReadMessage() |
| 95 | if err != nil { |
| 96 | errc <- err |
| 97 | return |
| 98 | } |
| 99 | if !strings.HasPrefix(string(inMsg), `{"tag":"_status"`) { |
| 100 | errc <- fmt.Errorf("unexpected message type=%d msg=%q, wanted status update", inType, inMsg) |
| 101 | return |
| 102 | } |
| 103 | inType, inMsg, err = wc.ReadMessage() |
| 104 | if err != nil { |
| 105 | errc <- err |
| 106 | return |
| 107 | } |
| 108 | if strings.Contains(string(inMsg), pn.String()) { |
| 109 | errc <- nil |
| 110 | return |
| 111 | } |
| 112 | errc <- fmt.Errorf("unexpected message type=%d msg=%q", inType, inMsg) |
| 113 | }() |
| 114 | select { |
| 115 | case err := <-errc: |
| 116 | if err != nil { |
nothing calls this directly
no test coverage detected