Skip to content

Commit

Permalink
move all header type and functions to new header.go file
Browse files Browse the repository at this point in the history
  • Loading branch information
Jirawat Harnsiriwatanakit committed Jan 25, 2020
1 parent ae0ae39 commit 756091e
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 73 deletions.
41 changes: 41 additions & 0 deletions header.go
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
}

47 changes: 47 additions & 0 deletions header_test.go
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")
}
}
33 changes: 0 additions & 33 deletions req.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}

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

0 comments on commit 756091e

Please sign in to comment.