newRequest is a helper function to create a new request with a method and url. The request returned is a 'server' request as opposed to a 'client' one through simulated write onto the wire and read off of the wire. The differences between requests are detailed in the net/http package.
(method, url string)
| 2956 | // simulated write onto the wire and read off of the wire. |
| 2957 | // The differences between requests are detailed in the net/http package. |
| 2958 | func newRequest(method, url string) *http.Request { |
| 2959 | req, err := http.NewRequest(method, url, nil) |
| 2960 | if err != nil { |
| 2961 | panic(err) |
| 2962 | } |
| 2963 | // extract the escaped original host+path from url |
| 2964 | // http://localhost/path/here?v=1#frag -> //localhost/path/here |
| 2965 | opaque := "" |
| 2966 | if i := len(req.URL.Scheme); i > 0 { |
| 2967 | opaque = url[i+1:] |
| 2968 | } |
| 2969 | |
| 2970 | if i := strings.LastIndex(opaque, "?"); i > -1 { |
| 2971 | opaque = opaque[:i] |
| 2972 | } |
| 2973 | if i := strings.LastIndex(opaque, "#"); i > -1 { |
| 2974 | opaque = opaque[:i] |
| 2975 | } |
| 2976 | |
| 2977 | // Escaped host+path workaround as detailed in https://golang.org/pkg/net/url/#URL |
| 2978 | // for < 1.5 client side workaround |
| 2979 | req.URL.Opaque = opaque |
| 2980 | |
| 2981 | // Simulate writing to wire |
| 2982 | var buff bytes.Buffer |
| 2983 | err = req.Write(&buff) |
| 2984 | if err != nil { |
| 2985 | log.Printf("Failed writing HTTP request: %v", err) |
| 2986 | } |
| 2987 | ioreader := bufio.NewReader(&buff) |
| 2988 | |
| 2989 | // Parse request off of 'wire' |
| 2990 | req, err = http.ReadRequest(ioreader) |
| 2991 | if err != nil { |
| 2992 | panic(err) |
| 2993 | } |
| 2994 | return req |
| 2995 | } |
| 2996 | |
| 2997 | // create a new request with the provided headers |
| 2998 | func newRequestWithHeaders(method, url string, headers ...string) *http.Request { |
no test coverage detected