diff --git a/header.go b/header.go new file mode 100644 index 00000000..ac1be32b --- /dev/null +++ b/header.go @@ -0,0 +1,41 @@ +/* + GoLang code created by Jirawat Harnsiriwatanakit https://github.com/kazekim +*/ + +package req + +import "encoding/json" + +// Header represents http request header +type Header map[string]string + +func (h Header) Clone() Header { + if h == nil { + return nil + } + hh := Header{} + for k, v := range h { + hh[k] = v + } + return hh +} + +// ParseStruct parse struct into header +func ParseStruct(h Header, v interface{}) Header { + data, err := json.Marshal(v) + if err != nil { + return h + } + + err = json.Unmarshal(data, &h) + return h +} + +// HeaderFromStruct init header from struct +func HeaderFromStruct(v interface{}) Header { + + var header Header + header = ParseStruct(header, v) + return header +} + diff --git a/header_test.go b/header_test.go new file mode 100644 index 00000000..5e56fac4 --- /dev/null +++ b/header_test.go @@ -0,0 +1,47 @@ +/* + GoLang code created by Jirawat Harnsiriwatanakit https://github.com/kazekim +*/ + +package req + +import "testing" + +func TestParseStruct(t *testing.T) { + + type HeaderStruct struct { + UserAgent string `json:"User-Agent"` + Authorization string `json:"Authorization"` + } + + h := HeaderStruct{ + "V1.0.0", + "roc", + } + + var header Header + header = ParseStruct(header, h) + + if header["User-Agent"] != h.UserAgent && header["Authorization"] != h.Authorization { + t.Fatal("struct parser for header is not working") + } + +} + +func TestHeaderFromStruct(t *testing.T) { + + type HeaderStruct struct { + UserAgent string `json:"User-Agent"` + Authorization string `json:"Authorization"` + } + + h := HeaderStruct{ + "V1.0.0", + "roc", + } + + header := HeaderFromStruct(h) + + if header["User-Agent"] != h.UserAgent && header["Authorization"] != h.Authorization { + t.Fatal("struct parser for header is not working") + } +} diff --git a/req.go b/req.go index b0424a84..01937f04 100644 --- a/req.go +++ b/req.go @@ -34,39 +34,6 @@ const ( LstdFlags = LreqHead | LreqBody | LrespHead | LrespBody ) -// Header represents http request header -type Header map[string]string - -func (h Header) Clone() Header { - if h == nil { - return nil - } - hh := Header{} - for k, v := range h { - hh[k] = v - } - return hh -} - -// ParseStruct parse struct into header -func ParseStruct(h Header, v interface{}) Header { - data, err := json.Marshal(v) - if err != nil { - return h - } - - err = json.Unmarshal(data, &h) - return h -} - -// HeaderFromStruct init header from struct -func HeaderFromStruct(v interface{}) Header { - - var header Header - header = ParseStruct(header, v) - return header -} - // Param represents http request param type Param map[string]interface{} diff --git a/req_test.go b/req_test.go index f37ad4fe..a863941a 100644 --- a/req_test.go +++ b/req_test.go @@ -267,46 +267,6 @@ func TestHeader(t *testing.T) { } } -func TestParseStruct(t *testing.T) { - - type HeaderStruct struct { - UserAgent string `json:"User-Agent"` - Authorization string `json:"Authorization"` - } - - h := HeaderStruct{ - "V1.0.0", - "roc", - } - - var header Header - header = ParseStruct(header, h) - - if header["User-Agent"] != "V1.0.0" && header["Authorization"] != "roc" { - t.Fatal("struct parser for header is not working") - } - -} - -func TestHeaderFromStruct(t *testing.T) { - - type HeaderStruct struct { - UserAgent string `json:"User-Agent"` - Authorization string `json:"Authorization"` - } - - h := HeaderStruct{ - "V1.0.0", - "roc", - } - - header := HeaderFromStruct(h) - - if header["User-Agent"] != "V1.0.0" && header["Authorization"] != "roc" { - t.Fatal("struct parser for header is not working") - } -} - func TestUpload(t *testing.T) { str := "hello req" file := ioutil.NopCloser(strings.NewReader(str))