From 2684f8ea2bcca9cb3e8b4114596bea4d686b6731 Mon Sep 17 00:00:00 2001 From: monaco Date: Tue, 9 Feb 2021 15:06:30 +0800 Subject: [PATCH] feat: sorted header --- context/content.go | 7 +++++-- model.go | 3 +++ request.go | 1 + request/header.go | 20 ++++++++++++++++++++ request/request.go | 6 ++++++ request_test.go | 19 +++++++++++++++++++ version.go | 2 +- 7 files changed, 55 insertions(+), 3 deletions(-) diff --git a/context/content.go b/context/content.go index 9a098c5..52d6fa2 100644 --- a/context/content.go +++ b/context/content.go @@ -1,6 +1,8 @@ package context -import "strings" +import ( + "strings" +) type contentType int @@ -37,5 +39,6 @@ func (ctx *Context) SetContentType(ct contentType) { // ContentTypeValid ... func ContentTypeValid(current string, ct contentType) bool { - return strings.HasPrefix(current, ContentTypes[ct]) + prefixIndex := strings.Index(ContentTypes[ct], ";") + return strings.HasPrefix(current, ContentTypes[ct][:prefixIndex]) } diff --git a/model.go b/model.go index 37fb373..8b53d42 100644 --- a/model.go +++ b/model.go @@ -30,6 +30,9 @@ type Client struct { // Header http header Header map[string]string + // SortedHeader http sorted header, example: [][2]string{{"h1": "v1"}, {"h2": "v2"}} + SortedHeader [][2]string + // Query params on http url Query map[string]string diff --git a/request.go b/request.go index c5bcaba..7c2f9a3 100644 --- a/request.go +++ b/request.go @@ -16,6 +16,7 @@ func (c *Client) Send() *response.Sugar { request.Query{Data: c.Query}, request.Method{Data: c.Method}, request.Header{Data: c.Header}, + request.SortedHeader{Data: c.SortedHeader}, request.UserAgent{Version: Version}, request.Cookies{Data: c.Cookies, Map: c.CookiesMap}, request.BearerAuth{Data: c.Bearer}, diff --git a/request/header.go b/request/header.go index 7bd1ff7..58b78aa 100644 --- a/request/header.go +++ b/request/header.go @@ -24,6 +24,26 @@ func (h Header) Valid() bool { return true } +// SortedHeader header slice, example [][2]string{{k1,v1},{k2,v2}} +type SortedHeader struct { + Data [][2]string +} + +// Apply apply http headers +func (h SortedHeader) Apply(ctx *context.Context) { + for i := range h.Data { + ctx.Request.Header.Set(h.Data[i][0], h.Data[i][1]) + } +} + +// Valid user agent in header valid? +func (h SortedHeader) Valid() bool { + if h.Data == nil { + return false + } + return true +} + // UserAgent user agent in header type UserAgent struct { Version string diff --git a/request/request.go b/request/request.go index d41cb9e..a9b5786 100644 --- a/request/request.go +++ b/request/request.go @@ -101,6 +101,12 @@ func (r *Request) AddHeader(data map[string]string) *Request { use(Header{Data: data}) } +// AddSortedHeader ... +func (r *Request) AddSortedHeader(data [][2]string) *Request { + return r. + use(SortedHeader{Data: data}) +} + // AddBasicAuth ... func (r *Request) AddBasicAuth(username, password string) *Request { return r. diff --git a/request_test.go b/request_test.go index 9c954c1..d7c937b 100644 --- a/request_test.go +++ b/request_test.go @@ -13,6 +13,7 @@ func TestClient_Send(t *testing.T) { URL string Method string Header map[string]string + SortedHeader [][2]string Query map[string]string JSON interface{} XML interface{} @@ -51,6 +52,23 @@ func TestClient_Send(t *testing.T) { Transport: &http.Transport{}, }, }, + { + name: "SortedHeader", + fields: fields{ + URL: "http://httpbin.org/post", + Method: POST, + SortedHeader: [][2]string{{"A", "A"}, {"B", "B"}, {"C", "C"}}, + Query: map[string]string{"google": "google"}, + JSON: map[string]string{"google": "google"}, + BasicAuth: BasicAuth{Username: "google", Password: "google"}, + Timeout: time.Second * 10, + TLSTimeout: time.Second * 10, + DialTimeout: time.Second * 10, + CookiesMap: map[string]string{"google": "google"}, + TLSConfig: &tls.Config{}, + Transport: &http.Transport{}, + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -58,6 +76,7 @@ func TestClient_Send(t *testing.T) { URL: tt.fields.URL, Method: tt.fields.Method, Header: tt.fields.Header, + SortedHeader: tt.fields.SortedHeader, Query: tt.fields.Query, JSON: tt.fields.JSON, XML: tt.fields.XML, diff --git a/version.go b/version.go index 71dbb53..92c5be7 100644 --- a/version.go +++ b/version.go @@ -1,4 +1,4 @@ package request // Version of module github.com/monaco-io/request -const Version = "v1.0.7" +const Version = "v1.0.9"