本文整理汇总了Golang中net/http.Response.Header方法的典型用法代码示例。如果您正苦于以下问题:Golang Response.Header方法的具体用法?Golang Response.Header怎么用?Golang Response.Header使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类net/http.Response
的用法示例。
在下文中一共展示了Response.Header方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestRateLimitUpdating
func TestRateLimitUpdating(t *testing.T) {
var resp http.Response
resp.StatusCode = 200
resp.Header = http.Header{"Date": []string{"Tue, 10 Oct 2013 20:11:05 GMT"}, "X-Ratelimit-Limit": []string{"600,30000"}, "X-Ratelimit-Usage": []string{"300,10000"}}
RateLimiting.updateRateLimits(&resp)
if RateLimiting.RequestTime.IsZero() {
t.Errorf("rate limiting should set request time")
}
if v := RateLimiting.FractionReached(); v != 0.5 {
t.Errorf("fraction of rate limit computed incorrectly, got %v", v)
}
resp.Header = http.Header{"Date": []string{"Tue, 10 Oct 2013 20:11:05 GMT"}, "X-Ratelimit-Limit": []string{"600,30000"}, "X-Ratelimit-Usage": []string{"300,27000"}}
RateLimiting.updateRateLimits(&resp)
if RateLimiting.RequestTime.IsZero() {
t.Errorf("rate limiting should set request time")
}
if v := RateLimiting.FractionReached(); v != 0.9 {
t.Errorf("fraction of rate limit computed incorrectly, got %v", v)
}
// we'll feed it nonsense
resp.Header = http.Header{"Date": []string{"Tue, 10 Oct 2013 20:11:05 GMT"}, "X-Ratelimit-Limit": []string{"xxx"}, "X-Ratelimit-Usage": []string{"zzz"}}
RateLimiting.updateRateLimits(&resp)
if !RateLimiting.RequestTime.IsZero() {
t.Errorf("nonsense in rate limiting fields should set next reset to zero")
}
}
示例2: SimpleResponse
func SimpleResponse(req *http.Request, status int, headers http.Header, body string) *http.Response {
res := new(http.Response)
body_rdr := (*fixedResBody)(strings.NewReader(body))
res.StatusCode = status
res.ProtoMajor = 1
res.ProtoMinor = 1
res.ContentLength = int64((*strings.Reader)(body_rdr).Len())
res.Request = req
res.Header = make(map[string][]string)
res.Body = body_rdr
if headers != nil {
res.Header = headers
}
return res
}
示例3: TestGetUser
func TestGetUser(t *testing.T) {
g := New("clientID", "secret", "http://myapp.com/")
creds := &common.Credentials{Map: objx.MSI()}
testTripperFactory := new(test.TestTripperFactory)
testTripper := new(test.TestTripper)
testTripperFactory.On("NewTripper", mock.Anything, g).Return(testTripper, nil)
testResponse := new(http.Response)
testResponse.Header = make(http.Header)
testResponse.Header.Set("Content-Type", "application/json")
testResponse.StatusCode = 200
testResponse.Body = ioutil.NopCloser(strings.NewReader(`{"name":"their-name","id":"uniqueid","login":"loginname","email":"[email protected]","picture":"http://myface.com/","blog":"http://blog.com/"}`))
testTripper.On("RoundTrip", mock.Anything).Return(testResponse, nil)
g.tripperFactory = testTripperFactory
user, err := g.GetUser(creds)
if assert.NoError(t, err) && assert.NotNil(t, user) {
assert.Equal(t, user.Name(), "their-name")
assert.Equal(t, user.AuthCode(), "") // doesn't come from google
assert.Equal(t, user.Email(), "[email protected]")
assert.Equal(t, user.AvatarURL(), "http://myface.com/")
assert.Equal(t, user.Data()["blog"], "http://blog.com/")
googleCreds := user.ProviderCredentials()[googleName]
if assert.NotNil(t, googleCreds) {
assert.Equal(t, "uniqueid", googleCreds.Get(common.CredentialsKeyID).Str())
}
}
}
示例4: TestNtlmHeaderParseInvalid
func TestNtlmHeaderParseInvalid(t *testing.T) {
res := http.Response{}
res.Header = make(map[string][]string)
res.Header.Add("Www-Authenticate", base64.StdEncoding.EncodeToString([]byte("NTLM I am a moose")))
_, err := parseChallengeResponse(&res)
assert.NotEqual(t, err, nil)
}
示例5: TestGetUser
func TestGetUser(t *testing.T) {
testProvider := new(test.TestProvider)
creds := new(common.Credentials)
testTripperFactory := new(test.TestTripperFactory)
testTripper := new(test.TestTripper)
testTripperFactory.On("NewTripper", creds, testProvider).Return(testTripper, nil)
testResponse := new(http.Response)
testResponse.Header = make(http.Header)
testResponse.Header.Set("Content-Type", "application/json")
testResponse.StatusCode = 200
testResponse.Body = ioutil.NopCloser(strings.NewReader(`{"name":"their-name","id":"uniqueid","login":"loginname","email":"[email protected]","avatar_url":"http://myface.com/","blog":"http://blog.com/"}`))
testTripper.On("RoundTrip", mock.Anything).Return(testResponse, nil)
client := &http.Client{Transport: testTripper}
testProvider.On("GetClient", creds).Return(client, nil)
data, err := Get(testProvider, creds, "endpoint")
if assert.NoError(t, err) && assert.NotNil(t, data) {
assert.Equal(t, data["name"], "their-name")
assert.Equal(t, data["id"], "uniqueid")
assert.Equal(t, data["login"], "loginname")
assert.Equal(t, data["email"], "[email protected]")
assert.Equal(t, data["avatar_url"], "http://myface.com/")
assert.Equal(t, data["blog"], "http://blog.com/")
}
}
示例6: TestOAuth2Provider_Non200Response
func TestOAuth2Provider_Non200Response(t *testing.T) {
config := &common.Config{
Map: objx.MSI(
OAuth2KeyRedirectUrl, OAuth2KeyRedirectUrl,
OAuth2KeyScope, OAuth2KeyScope,
OAuth2KeyClientID, OAuth2KeyClientID,
OAuth2KeySecret, OAuth2KeySecret,
OAuth2KeyAuthURL, OAuth2KeyAuthURL,
OAuth2KeyTokenURL, OAuth2KeyTokenURL)}
testTripperFactory := new(test.TestTripperFactory)
testTripper := new(test.TestTripper)
testProvider := new(test.TestProvider)
testResponse := new(http.Response)
testResponse.Header = make(http.Header)
testResponse.Header.Set("Content-Type", "text/plain")
testResponse.StatusCode = 401
testResponse.Body = ioutil.NopCloser(strings.NewReader("No mate"))
testTripperFactory.On("NewTripper", common.EmptyCredentials, mock.Anything).Return(testTripper, nil)
testTripper.On("RoundTrip", mock.Anything).Return(testResponse, nil)
data := objx.MSI(OAuth2KeyCode, []string{"123"})
_, err := CompleteAuth(testTripperFactory, data, config, testProvider)
if assert.Error(t, err) {
assert.IsType(t, &common.AuthServerError{}, err)
}
mock.AssertExpectationsForObjects(t, testTripperFactory.Mock, testTripper.Mock, testProvider.Mock)
}
示例7: TestGetUser
func TestGetUser(t *testing.T) {
g := New("clientID", "secret", "http://myapp.com/")
creds := &common.Credentials{Map: objx.MSI()}
testTripperFactory := new(test.TestTripperFactory)
testTripper := new(test.TestTripper)
testTripperFactory.On("NewTripper", mock.Anything, g).Return(testTripper, nil)
testResponse := new(http.Response)
testResponse.Header = make(http.Header)
testResponse.Header.Set("Content-Type", "application/json")
testResponse.StatusCode = 200
testResponse.Body = ioutil.NopCloser(strings.NewReader(`{"full_name":"their-name","id":"uniqueid","username":"loginname","avatar_url":"http://myface.com/","online":true}`))
testTripper.On("RoundTrip", mock.Anything).Return(testResponse, nil)
g.tripperFactory = testTripperFactory
user, err := g.GetUser(creds)
if assert.NoError(t, err) && assert.NotNil(t, user) {
assert.Equal(t, user.Name(), "their-name")
assert.Equal(t, user.AuthCode(), "")
assert.Equal(t, user.Nickname(), "loginname")
assert.Equal(t, user.Email(), "") // doesn't come from soundcloud
assert.Equal(t, user.AvatarURL(), "http://myface.com/")
assert.Equal(t, user.Data()["online"], true)
}
}
示例8: NewTestResponse
func NewTestResponse(status float64, data interface{}, errors []map[string]interface{}, context string, changeInfo api.ChangeInfo) *api.Response {
httpResponse := new(http.Response)
sro := map[string]interface{}{common.ResponseObjectFieldStatusCode: status,
common.ResponseObjectFieldData: data,
common.ResponseObjectFieldErrors: errors,
common.ResponseObjectFieldChangeInfo: changeInfo,
common.ResponseObjectFieldContext: context}
session := api.NewSession("project", "company", "apiKey")
responseBytes, _ := session.Codec().Marshal(sro, nil)
httpResponse.Body = ioutil.NopCloser(bytes.NewBuffer(responseBytes))
httpResponse.Header = make(map[string][]string)
response, newResponseErr := api.NewResponse(session, httpResponse)
if newResponseErr != nil {
panic(fmt.Sprintf("NewTestResponse: %s", newResponseErr))
}
return response
}
示例9: SetResponseHeaderValues
// SetResponseHeaderValues adds a header containing all the passed string values.
func SetResponseHeaderValues(resp *http.Response, h string, values []string) {
if resp.Header == nil {
resp.Header = make(http.Header)
}
for _, v := range values {
resp.Header.Add(h, v)
}
}
示例10: TestNtlmHeaderParseValid
func TestNtlmHeaderParseValid(t *testing.T) {
res := http.Response{}
res.Header = make(map[string][]string)
res.Header.Add("Www-Authenticate", "NTLM "+base64.StdEncoding.EncodeToString([]byte("I am a moose")))
bytes, err := parseChallengeResponse(&res)
assert.Equal(t, err, nil)
assert.Equal(t, strings.HasPrefix(string(bytes), "NTLM"), false)
}
示例11: readFakeResponse
func readFakeResponse(statusCode int, file string, header http.Header) (res http.Response) {
body, _ := os.Open(file)
response := http.Response{}
response.Body = body
response.Header = header
response.StatusCode = statusCode
return response
}
示例12: SimpleResponse
// Generate an http.Response using the basic fields
func SimpleResponse(req *http.Request, status int, headers http.Header, contentLength int64, body io.Reader) *http.Response {
res := new(http.Response)
res.StatusCode = status
res.ProtoMajor = 1
res.ProtoMinor = 1
res.ContentLength = contentLength
res.Request = req
res.Header = make(map[string][]string)
if body_rdr, ok := body.(io.ReadCloser); ok {
res.Body = body_rdr
} else if body != nil {
res.Body = ioutil.NopCloser(body)
}
if headers != nil {
res.Header = headers
}
return res
}
示例13: TestResponse
func TestResponse(t *testing.T) {
var resp http.Response
resp.StatusCode = http.StatusOK
resp.Header = make(http.Header)
resp.Header["SID"] = []string{"uuid:1337"}
var buf bytes.Buffer
resp.Write(&buf)
t.Logf("%q", buf.String())
}
示例14: Restore
func (c *URLCache) Restore(res *http.Response) {
res.Status = c.CachedResponse.Status
res.StatusCode = c.CachedResponse.StatusCode
res.Header = c.CachedResponse.Header
res.ContentLength = c.CachedResponse.ContentLength
res.TransferEncoding = c.CachedResponse.TransferEncoding
res.Body = &ClosableBuffer{bytes.NewReader(c.CachedBody)}
res.Header.Set(CachedHeader, CachedHeaderVal)
res.Header.Set(CachedMD5Header, c.MD5)
}
示例15: MakeTestResponseWithBody
func MakeTestResponseWithBody(body string) (*Response, error) {
testHTTPResponse := new(http.Response)
testHTTPResponse.Body = ioutil.NopCloser(bytes.NewBufferString(body))
testHTTPResponse.StatusCode = 200
testHTTPResponse.Header = make(map[string][]string)
return NewResponse(GetTestSession(), testHTTPResponse)
}