Skip to content

Commit

Permalink
feat: sorted header
Browse files Browse the repository at this point in the history
  • Loading branch information
luxuze committed Feb 9, 2021
1 parent c698195 commit 2684f8e
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 3 deletions.
7 changes: 5 additions & 2 deletions context/content.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package context

import "strings"
import (
"strings"
)

type contentType int

Expand Down Expand Up @@ -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])
}
3 changes: 3 additions & 0 deletions model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
20 changes: 20 additions & 0 deletions request/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions request/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
19 changes: 19 additions & 0 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down Expand Up @@ -51,13 +52,31 @@ 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) {
c := &Client{
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,
Expand Down
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package request

// Version of module github.com/monaco-io/request
const Version = "v1.0.7"
const Version = "v1.0.9"

0 comments on commit 2684f8e

Please sign in to comment.