本文整理汇总了Golang中net/textproto.CanonicalMIMEHeaderKey函数的典型用法代码示例。如果您正苦于以下问题:Golang CanonicalMIMEHeaderKey函数的具体用法?Golang CanonicalMIMEHeaderKey怎么用?Golang CanonicalMIMEHeaderKey使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了CanonicalMIMEHeaderKey函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestGetForwardedIpAndTimeStamp
func TestGetForwardedIpAndTimeStamp(t *testing.T) {
testIp := net.ParseIP("222.222.222.222")
testTime := time.Unix(1397768380, 0)
headers := map[string][]string{
textproto.CanonicalMIMEHeaderKey("X-ORIGINAL-MSEC"): []string{fmt.Sprintf("%d.000", testTime.Unix())},
textproto.CanonicalMIMEHeaderKey("X-Forwarded-For"): []string{"222.222.222.222, 123.123.123.123, 123.123.123.124"},
}
ip := getIpFromHeader("X-Forwarded-For", headers)
if testIp.String() != ip.String() {
t.Errorf("Expecting %s for ip got %s\n", testIp, ip)
}
}
示例2: getHeader
func getHeader(i map[string][]string, header string) string {
h, ok := textproto.MIMEHeader(i)[textproto.CanonicalMIMEHeaderKey(header)]
if ok {
return h[0]
}
return ""
}
示例3: AssertJSONCall
// AssertJSONCall asserts that when the given handler is called with
// the given parameters, the result is as specified.
func AssertJSONCall(c *gc.C, p JSONCallParams) {
c.Logf("JSON call, url %q", p.URL)
if p.ExpectStatus == 0 {
p.ExpectStatus = http.StatusOK
}
rec := DoRequest(c, DoRequestParams{
Do: p.Do,
ExpectError: p.ExpectError,
Handler: p.Handler,
Method: p.Method,
URL: p.URL,
Body: p.Body,
JSONBody: p.JSONBody,
Header: p.Header,
ContentLength: p.ContentLength,
Username: p.Username,
Password: p.Password,
Cookies: p.Cookies,
})
if p.ExpectError != "" {
return
}
AssertJSONResponse(c, rec, p.ExpectStatus, p.ExpectBody)
for k, v := range p.ExpectHeader {
c.Assert(rec.HeaderMap[textproto.CanonicalMIMEHeaderKey(k)], gc.DeepEquals, v, gc.Commentf("header %q", k))
}
}
示例4: applyOverrides
// applyOverrides applies the transport's request overrides to req. If any
// overrides apply, req is cloned and the overrides are applied to the clone.
func (t *RequestModifyingTransport) applyOverrides(req *http.Request) *http.Request {
// Only override GET and HEAD requests, just to be safe. We may want to
// revisit this constraint later.
if method := strings.ToUpper(req.Method); method != "GET" && method != "HEAD" {
return req
}
requestURI := req.URL.RequestURI()
t.overridesMu.Lock()
defer t.overridesMu.Unlock()
cloned := false
for requestURIRegexp, override := range t.overrides {
if requestURIRegexp.MatchString(requestURI) {
if !cloned {
req = cloneRequest(req)
cloned = true
}
for name, val := range override.setHeaders {
req.Header[textproto.CanonicalMIMEHeaderKey(name)] = val
}
if override.runOnlyOnce {
delete(t.overrides, requestURIRegexp)
}
}
}
return req
}
示例5: HaveHeader
// heaveHeader check the existence of header header
func (m *Message) HaveHeader(key string) bool {
key = textproto.CanonicalMIMEHeaderKey(key)
if len(m.Header.Get(key)) == 0 {
return false
}
return true
}
示例6: TestContentHashHeaderCanonicalized
func (*suite) TestContentHashHeaderCanonicalized(c *gc.C) {
// The header key should be canonicalized, because otherwise
// the actually produced header will be different from that
// specified.
canon := textproto.CanonicalMIMEHeaderKey(params.ContentHashHeader)
c.Assert(canon, gc.Equals, params.ContentHashHeader)
}
示例7: IsSet
// IsSet tests if a key is present in the Header
func (h Header) IsSet(key string) bool {
if h == nil {
return false
}
_, ok := h[textproto.CanonicalMIMEHeaderKey(key)]
return ok
}
示例8: Get
// Get gets the first value associated with key in the Header and
// returns it and true. If the key is unset it returns the empty string
// and false.
func (h Header) Get(key string) (string, bool) {
v, ok := h[textproto.CanonicalMIMEHeaderKey(key)]
if !ok {
return "", false
}
return v[0], ok
}
示例9: TestSetHeaders
func TestSetHeaders(t *testing.T) {
expectedFromEmptyHeader := http.Header{
textproto.CanonicalMIMEHeaderKey(openrtb.HEADER_VERSION): []string{openrtb.VERSION},
"Content-Type": []string{"application/json; charset=utf-8"},
}
tests := []struct {
input http.Header
expected http.Header
}{
// Should set from empty header.
{
http.Header{},
expectedFromEmptyHeader,
},
// Should replace the old content type.
{
http.Header{"Content-Type": []string{"text/plain"}},
expectedFromEmptyHeader,
},
// Should replace the old OpenRTB spec version.
{
http.Header{textproto.CanonicalMIMEHeaderKey(openrtb.HEADER_VERSION): []string{"2.1"}},
expectedFromEmptyHeader,
},
// Should keep existing, non-overlapped headers.
{
http.Header{"X-Additional-Header": []string{"for test"}},
http.Header{
textproto.CanonicalMIMEHeaderKey(openrtb.HEADER_VERSION): []string{openrtb.VERSION},
"Content-Type": []string{"application/json; charset=utf-8"},
"X-Additional-Header": []string{"for test"},
},
},
}
for i, test := range tests {
t.Logf("Testint %d...", i)
openrtb.SetHeaders(test.input)
if !reflect.DeepEqual(test.input, test.expected) {
t.Errorf("Expected the HTTP headers to be\n%v instead of\n%v.", test.expected, test.input)
}
}
}
示例10: NewObjectFromStream
// NewObjectFromStream ...
func NewObjectFromStream(header textproto.MIMEHeader, body io.ReadCloser) (*Object, error) {
objectID, err := strconv.ParseInt(header.Get("Object-ID"), 10, 64)
if err != nil {
// Attempt to parse a Rets Response code (if it exists)
resp, parseErr := ReadResponse(body)
if parseErr != nil {
return nil, err
}
// Include a GetObject (empty of content) so that its rets response can be retrieved
emptyResult := Object{
RetsMessage: resp,
RetsError: resp.Code != StatusOK,
}
return &emptyResult, err
}
preferred, err := strconv.ParseBool(header.Get("Preferred"))
if err != nil {
preferred = false
}
objectData := make(map[string]string)
for _, v := range header[textproto.CanonicalMIMEHeaderKey("ObjectData")] {
kv := strings.Split(v, "=")
objectData[kv[0]] = kv[1]
}
blob, err := ioutil.ReadAll(body)
if err != nil {
return nil, err
}
// 5.6.7
retsError, err := strconv.ParseBool(header.Get("RETS-Error"))
retsMsg, err := ReadResponse(ioutil.NopCloser(bytes.NewReader(blob)))
// there is a rets message, stash it and wipe the content
if err == nil {
blob = nil
}
object := Object{
// required
ObjectID: int(objectID),
ContentID: header.Get("Content-ID"),
ContentType: header.Get("Content-Type"),
// optional
UID: header.Get("UID"),
Description: header.Get("Content-Description"),
SubDescription: header.Get("Content-Sub-Description"),
Location: header.Get("Location"),
RetsError: retsError,
RetsMessage: retsMsg,
Preferred: preferred,
ObjectData: objectData,
Blob: blob,
}
return &object, nil
}
示例11: GetHeaders
// GetHeaders returns valueS for header key key
func (m *Email) GetHeaders(key string) (headers []string, err error) {
// if not parsed
if !m.flagHeaderParsed {
if err = m.parseHeader(); err != nil {
return headers, err
}
}
headers, _ = m.Header[textproto.CanonicalMIMEHeaderKey(key)]
return
}
示例12: Get
// Get gets the first value associated with the given key.
// If there are no values associated with the key, Get returns "".
// Get is a convenience method. For more complex queries,
// access the map directly.
func (h Header) Get(key string) string {
if h == nil {
return ""
}
v := h[textproto.CanonicalMIMEHeaderKey(key)]
if len(v) == 0 {
return ""
}
return v[0]
}
示例13: TestCompile
func TestCompile(t *testing.T) {
pat, err := Compile("hello, %% %b %D %h %H %l %m %p %q %r %s %t %T %u %U %v %V %>s %{X-LogFormat-Test}i %{X-LogFormat-Test}o world!")
if err != nil {
t.Errorf("Failed to compile: %s", err)
return
}
b := &bytes.Buffer{}
pat(b, dummyCtx{
elapsed: 5 * time.Second,
req: &http.Request{
Header: http.Header{
textproto.CanonicalMIMEHeaderKey("Content-Length"): []string{"8192"},
textproto.CanonicalMIMEHeaderKey("X-LogFormat-Test"): []string{"Hello, Request!"},
},
Method: "GET",
Proto: "HTTP/1.1",
RemoteAddr: "192.168.11.1",
Host: "example.com",
URL: &url.URL{
Host: "example.com",
Path: "/hello_world",
RawQuery: "hello=world",
},
},
res: &dummyResponse{
hdrs: http.Header{
textproto.CanonicalMIMEHeaderKey("X-LogFormat-Test"): []string{"Hello, Response!"},
},
status: 400,
},
})
re := regexp.MustCompile(`^hello, % 8192 5000000 192\.168\.11\.1 HTTP/1\.1 - GET \d+ \?hello=world GET //example\.com/hello_world\?hello=world HTTP/1\.1 400 \d{2}/[a-zA-Z]+/\d{4}:\d{2}:\d{2}:\d{2} [+-]\d{4} 5 - /hello_world example\.com example\.com 400 Hello, Request! Hello, Response! world!$`)
if !re.Match(b.Bytes()) {
t.Errorf("output did not match regexp")
t.Logf("output: %s", b.String())
t.Logf("regexp: %s", re)
return
}
}
示例14: doRequest
func doRequest(req *http.Request) *http.Response {
if _, ok := req.Header[textproto.CanonicalMIMEHeaderKey("User-Agent")]; !ok {
// Setting a blank User-Agent causes the http lib not to output one, whereas if there
// is no header, it will output a default one.
// See: https://code.google.com/p/go/source/browse/src/pkg/net/http/request.go?name=go1.3.3#398
req.Header.Set("User-Agent", "")
}
resp, err := http.DefaultTransport.RoundTrip(req)
Expect(err).To(BeNil())
return resp
}
示例15: GetHeaderSSE
// Returns the header value for server-side encryption, or empty string if there is no SSE header.
func GetHeaderSSE(headers map[string][]string) string {
// Headers returned in an HTTP response will have their key names connonicalized by the Go
// http library, so we MUST run the key through textproto.CanonicalMIMEHeaderKey in order
// to find it in the map
// So headers in the http request must also have been added with a cannonicalized key to find them.
// TODO: Change parameters in this file to use http.Header data type rather than map[string][]string
if val := headers[textproto.CanonicalMIMEHeaderKey(sseHeaderKey)]; len(val) > 0 {
return val[0]
}
return ""
}