github.com/bazelbuild/rules_webtesting@v0.2.0/go/wtl/proxy/driverhub/debugger/debugger_test.go (about) 1 // Copyright 2017 Google Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package debugger 16 17 import ( 18 "errors" 19 "io" 20 "io/ioutil" 21 "strings" 22 "testing" 23 ) 24 25 func TestBreakpointMatches(t *testing.T) { 26 testCases := []struct { 27 name string 28 breakpoint *breakpoint 29 request *request 30 matches bool 31 }{ 32 { 33 "empty breakpoint matches everything", 34 &breakpoint{}, 35 &request{}, 36 true, 37 }, 38 { 39 "matching method", 40 &breakpoint{ 41 Methods: []string{"POST", "GET"}, 42 }, 43 &request{ 44 Method: "POST", 45 }, 46 true, 47 }, 48 { 49 "non-matching method", 50 &breakpoint{ 51 Methods: []string{"POST", "GET"}, 52 }, 53 &request{ 54 Method: "DELETE", 55 }, 56 false, 57 }, 58 { 59 "matching path", 60 &breakpoint{ 61 Path: "url", 62 }, 63 &request{ 64 Path: "/wd/hub/session/abc/url", 65 }, 66 true, 67 }, 68 { 69 "non-matching path", 70 &breakpoint{ 71 Path: "url", 72 }, 73 &request{ 74 Path: "/wd/hub/session/abc/elements", 75 }, 76 false, 77 }, 78 { 79 "non-matching method and matching path", 80 &breakpoint{ 81 Methods: []string{"POST", "GET"}, 82 Path: "url", 83 }, 84 &request{ 85 Method: "DELETE", 86 Path: "/wd/hub/session/abc/url", 87 }, 88 false, 89 }, 90 { 91 "matching method and non-matching path", 92 &breakpoint{ 93 Methods: []string{"POST", "GET"}, 94 Path: "url", 95 }, 96 &request{ 97 Method: "POST", 98 Path: "/wd/hub/session/abc/elements", 99 }, 100 false, 101 }, 102 { 103 "non-matching method and matching path", 104 &breakpoint{ 105 Methods: []string{"POST", "GET"}, 106 Path: "url", 107 }, 108 &request{ 109 Method: "DELETE", 110 Path: "/wd/hub/session/abc/url", 111 }, 112 false, 113 }, 114 { 115 "non-matching method and path", 116 &breakpoint{ 117 Methods: []string{"POST", "GET"}, 118 Path: "url", 119 }, 120 &request{ 121 Method: "DELETE", 122 Path: "/wd/hub/session/abc/elements", 123 }, 124 false, 125 }, 126 { 127 "matching body", 128 &breakpoint{ 129 Body: `http://www\.google\.com`, 130 }, 131 &request{ 132 Body: `{"url": "http://www.google.com"}`, 133 }, 134 true, 135 }, 136 { 137 "non-matching body", 138 &breakpoint{ 139 Body: `http://www\.google\.com`, 140 }, 141 &request{ 142 Body: `{"url": "http://wwwagoogle.com"}`, 143 }, 144 false, 145 }, 146 } 147 148 for _, tc := range testCases { 149 t.Run(tc.name, func(t *testing.T) { 150 if err := tc.breakpoint.initialize(); err != nil { 151 t.Fatal(err) 152 } 153 if m := tc.breakpoint.matches(tc.request); m != tc.matches { 154 t.Fatalf("got %+v.matches(%+v) == %v, expected %v", tc.breakpoint, tc.request, m, tc.matches) 155 } 156 }) 157 } 158 } 159 160 type fakeReadCloser struct { 161 io.Reader 162 closed bool 163 } 164 165 func (f *fakeReadCloser) Close() error { 166 if f.closed { 167 return errors.New("Close called multiple times") 168 } 169 f.closed = true 170 return nil 171 } 172 173 func TestCapture(t *testing.T) { 174 f := &fakeReadCloser{ 175 Reader: strings.NewReader("a test string"), 176 } 177 178 c, err := capture(f) 179 if err != nil { 180 t.Fatal(err) 181 } 182 183 if c.captured != "a test string" { 184 t.Errorf(`Got c.captured == %q, expected "a test string"`, c.captured) 185 } 186 187 b, err := ioutil.ReadAll(c) 188 if err != nil { 189 t.Fatal(err) 190 } 191 if string(b) != "a test string" { 192 t.Errorf(`Got ioutil.ReadAll(c) == %q, expected "a test string"`, string(b)) 193 } 194 195 if err := c.Close(); err != nil { 196 t.Fatal(err) 197 } 198 199 if !f.closed { 200 t.Errorf("Got f.closed == false, expected true") 201 } 202 }