Commit 9554e671 authored by Brad Fitzpatrick's avatar Brad Fitzpatrick

http: fix transport crash when request URL is nil

Fixes #1602

R=rsc, petar-m
CC=golang-dev
https://golang.org/cl/4284043
parent e6c9bccd
......@@ -36,6 +36,11 @@ type transport struct {
}
func (ct *transport) Do(req *Request) (resp *Response, err os.Error) {
if req.URL == nil {
if req.URL, err = ParseURL(req.RawURL); err != nil {
return
}
}
if req.URL.Scheme != "http" && req.URL.Scheme != "https" {
return nil, &badStringError{"unsupported protocol scheme", req.URL.Scheme}
}
......
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Tests for transport.go
package http_test
import (
"fmt"
. "http"
"http/httptest"
"io/ioutil"
"testing"
)
func TestTransportNilURL(t *testing.T) {
ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
fmt.Fprintf(w, "Hi")
}))
defer ts.Close()
req := new(Request)
req.URL = nil // what we're actually testing
req.Method = "GET"
req.RawURL = ts.URL
req.Proto = "HTTP/1.1"
req.ProtoMajor = 1
req.ProtoMinor = 1
// TODO(bradfitz): test &transport{} and not DefaultTransport
// once Transport is exported.
res, err := DefaultTransport.Do(req)
if err != nil {
t.Fatalf("unexpected Do error: %v", err)
}
body, err := ioutil.ReadAll(res.Body)
if g, e := string(body), "Hi"; g != e {
t.Fatalf("Expected response body of %q; got %q", e, g)
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment