github.com/bazelbuild/bazel-watcher@v0.25.2/internal/e2e/live_reload/live_reload_test.go (about) 1 package live_reload 2 3 import ( 4 "io/ioutil" 5 "net/http" 6 "net/url" 7 "reflect" 8 "strings" 9 "testing" 10 "time" 11 12 "github.com/bazelbuild/bazel-watcher/internal/e2e" 13 "github.com/bazelbuild/rules_go/go/tools/bazel_testing" 14 "github.com/gorilla/websocket" 15 ) 16 17 const mainFiles = ` 18 -- BUILD -- 19 sh_binary( 20 name = "live_reload", 21 srcs = ["test.sh"], 22 # Add a simple data dependency that you can modify. 23 data = ["test.txt"], 24 tags = ["ibazel_live_reload"], 25 ) 26 sh_binary( 27 name = "no_live_reload", 28 srcs = ["test.sh"], 29 ) 30 -- test.txt -- 31 1 32 -- test.sh -- 33 printf "Live reload url: ${IBAZEL_LIVERELOAD_URL}" 34 ` 35 36 func TestMain(m *testing.M) { 37 bazel_testing.TestMain(m, bazel_testing.Args{ 38 Main: mainFiles, 39 }) 40 } 41 42 type liveReloadHello struct { 43 Command string `json:"command"` 44 Protocols []string `json:"protocols"` 45 } 46 47 func assertEqual(t *testing.T, want, got interface{}, msg string) { 48 t.Helper() 49 50 } 51 52 func verify(t *testing.T, conn *websocket.Conn, want interface{}) { 53 t.Helper() 54 55 conn.SetReadDeadline(time.Now().Add(5 * time.Second)) 56 57 _, v, err := conn.ReadMessage() 58 if err != nil { 59 t.Errorf("conn.ReadMessage(): %s", err) 60 } 61 62 got := strings.TrimSpace(string(v)) 63 64 if !reflect.DeepEqual(got, want) { 65 t.Errorf("websocket read diff: got [%v], want [%v]", got, want) 66 } 67 } 68 69 func TestLiveReload(t *testing.T) { 70 ibazel := e2e.SetUp(t) 71 ibazel.Run([]string{}, "//:live_reload") 72 defer ibazel.Kill() 73 74 ibazel.ExpectOutput("Live reload url: http://.+:\\d+") 75 out := ibazel.GetOutput() 76 t.Logf("Output: '%s'", out) 77 78 if out == "" { 79 t.Fatal("Output was empty. Expected at least some output") 80 } 81 82 jsUrl := out[len("Live reload url: "):] 83 t.Logf("Livereload URL: '%s'", jsUrl) 84 85 url, err := url.ParseRequestURI(jsUrl) 86 if err != nil { 87 t.Error(err) 88 } 89 90 client := &http.Client{} 91 resp, err := client.Get(jsUrl) 92 if err != nil { 93 t.Fatal(err) 94 } 95 defer resp.Body.Close() 96 97 body, err := ioutil.ReadAll(resp.Body) 98 if err != nil { 99 t.Fatal(err) 100 } 101 102 bodyString := string(body) 103 104 expectedStart := "(function e(t,n,r)" 105 actualStart := bodyString[0:len(expectedStart)] 106 if actualStart != expectedStart { 107 t.Errorf("Expected js to start with \"%s\" but got \"%s\".", expectedStart, actualStart) 108 } 109 110 expectedEnd := "},{}]},{},[8]);" 111 actualEnd := bodyString[len(bodyString)-len(expectedEnd):] 112 if actualEnd != expectedEnd { 113 t.Errorf("Expected js to end with \"%s\" but got \"%s\".", expectedEnd, actualEnd) 114 } 115 116 wsUrl := "ws://" + url.Hostname() + ":" + url.Port() + "/livereload" 117 t.Logf("wsUrl: %s", wsUrl) 118 conn, _, err := websocket.DefaultDialer.Dial(wsUrl, map[string][]string{}) 119 if err != nil { 120 t.Error(err) 121 } 122 123 // Send the hello message to the client. 124 hello := liveReloadHello{ 125 Command: "hello", 126 Protocols: []string{"http://livereload.com/protocols/official-7"}, 127 } 128 if err = conn.WriteJSON(hello); err != nil { 129 t.Error(err) 130 } 131 132 // Verify the hello message 133 verify(t, conn, `{"command":"hello","protocols":["http://livereload.com/protocols/official-7","http://livereload.com/protocols/official-8","http://livereload.com/protocols/official-9","http://livereload.com/protocols/2.x-origin-version-negotiation","http://livereload.com/protocols/2.x-remote-control"],"serverName":"live reload"}`) 134 135 e2e.MustWriteFile(t, "test.txt", "2") 136 ibazel.ExpectOutput("Live reload url: http://.+:\\d+") 137 138 verify(t, conn, `{"command":"reload","path":"reload","liveCSS":true}`) 139 140 e2e.MustWriteFile(t, "test.txt", "3") 141 ibazel.ExpectOutput("Live reload url: http://.+:\\d+") 142 143 verify(t, conn, `{"command":"reload","path":"reload","liveCSS":true}`) 144 } 145 146 func TestNoLiveReload(t *testing.T) { 147 ibazel := e2e.SetUp(t) 148 ibazel.Run([]string{}, "//:no_live_reload") 149 defer ibazel.Kill() 150 151 // Expect there to not be a url since this is the negative test case. 152 ibazel.ExpectOutput("Live reload url: $") 153 }