Skip to content

Commit db17eca

Browse files
authored
fix(devtool): handle trailing slash in URL and improve error message (#155)
A trailing slash in the URL caused double-slash paths like "//json/version", which failed with the misleading error "could not resolve IP for 127.0.0.1". Now we strip trailing slashes and show the actual endpoints tried on failure. Fixes #152
1 parent 3b1f600 commit db17eca

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

devtool/devtool.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ type DevTools struct {
3737

3838
// New returns a DevTools instance that uses URL.
3939
func New(url string, opts ...DevToolsOption) *DevTools {
40-
devtools := &DevTools{url: url}
40+
devtools := &DevTools{url: strings.TrimRight(url, "/")}
4141
for _, o := range opts {
4242
o(devtools)
4343
}
@@ -297,6 +297,7 @@ func (d *DevTools) resolveHost(ctx context.Context) error {
297297
return err
298298
}
299299

300+
var tried []string
300301
newURL := ""
301302
for _, a := range addrs {
302303
host[0] = a
@@ -305,7 +306,9 @@ func (d *DevTools) resolveHost(ctx context.Context) error {
305306

306307
// The selection of "/json/version" here is arbitrary,
307308
// it just needs to exist and not have side-effects.
308-
req, err := http.NewRequest(http.MethodGet, try+"/json/version", nil)
309+
endpoint := try + "/json/version"
310+
tried = append(tried, endpoint)
311+
req, err := http.NewRequest(http.MethodGet, endpoint, nil)
309312
if err != nil {
310313
return err
311314
}
@@ -322,7 +325,7 @@ func (d *DevTools) resolveHost(ctx context.Context) error {
322325
}
323326
}
324327
if newURL == "" {
325-
return errors.New("could not resolve IP for " + origHost)
328+
return fmt.Errorf("could not find a working endpoint for %q, tried: %v", origHost, tried)
326329
}
327330
d.url = newURL
328331

devtool/devtool_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,28 @@ func TestDevTools_InvalidURL(t *testing.T) {
253253
}
254254
}
255255

256+
func TestDevTools_TrailingSlash(t *testing.T) {
257+
th := newTestHandler(t)
258+
srv := httptest.NewServer(th)
259+
defer srv.Close()
260+
261+
// Create DevTools with trailing slash in URL.
262+
devt := New(srv.URL + "/")
263+
th.hostnameLookup = true
264+
265+
th.status = http.StatusOK
266+
th.body = read(t, filepath.Join("testdata", "version.json"))
267+
268+
ctx, cancel := context.WithCancel(context.Background())
269+
defer cancel()
270+
271+
// This should succeed without double-slash issues.
272+
_, err := devt.Version(ctx)
273+
if err != nil {
274+
t.Errorf("Version failed with trailing slash URL: %v", err)
275+
}
276+
}
277+
256278
func showDiff(t testing.TB, got, want []byte) {
257279
gr := bufio.NewReader(bytes.NewReader(got))
258280
wr := bufio.NewReader(bytes.NewReader(want))

0 commit comments

Comments
 (0)