forked from imroc/req
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move all header type and functions to new header.go file
- Loading branch information
Jirawat Harnsiriwatanakit
committed
Jan 25, 2020
1 parent
ae0ae39
commit 756091e
Showing
4 changed files
with
88 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters