示例#1
2
文件: web_test.go 项目: apg/web.go
func buildTestRequest(method string, path string, body string, headers map[string][]string, cookies []*http.Cookie) *http.Request {
	host := "127.0.0.1"
	port := "80"
	rawurl := "http://" + host + ":" + port + path
	url_, _ := url.Parse(rawurl)

	proto := "HTTP/1.1"

	if headers == nil {
		headers = map[string][]string{}
	}

	headers["User-Agent"] = []string{"web.go test"}
	if method == "POST" {
		headers["Content-Length"] = []string{fmt.Sprintf("%d", len(body))}
		if headers["Content-Type"] == nil {
			headers["Content-Type"] = []string{"text/plain"}
		}
	}

	req := http.Request{Method: method,
		RawURL: rawurl,
		URL:    url_,
		Proto:  proto,
		Host:   host,
		Header: http.Header(headers),
		Body:   ioutil.NopCloser(bytes.NewBufferString(body)),
	}

	for _, cookie := range cookies {
		req.AddCookie(cookie)
	}
	return &req
}
示例#2
1
func parseCookieHeader(cookieHeader string, request *http.Request) {
	// break up into individual cookies
	cookieStrings := strings.Split(cookieHeader, ";")

	for _, cookieString := range cookieStrings {
		var cookie *http.Cookie = new(http.Cookie)

		cookieStringSegments := strings.Split(strings.TrimSpace(cookieString), "=")

		cookie.Name = cookieStringSegments[0]
		cookie.Value = cookieStringSegments[1]

		request.AddCookie(cookie)
	}
}