Skip to content

Commit

Permalink
fixed options bug
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuzhenfeng-finogeeks committed Oct 30, 2018
1 parent a316a51 commit 87ba938
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 6 deletions.
16 changes: 16 additions & 0 deletions example/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package main

import (
"fmt"

"github.com/zzfup/go-fetch"
)

func main() {
url := "http://www.baidu.com"
resp, err := fetch.Fetch(url, fetch.Options{})
if err != nil {
fmt.Println(err)
}
fmt.Println(resp.ToString())
}
18 changes: 13 additions & 5 deletions fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,19 @@ import (
// url: Absolute url
// op: Fetch options
func Fetch(url string, op Options) (Resp, error) {
defaultOp := newDefaultOptions()
defaultOp.Method = op.Method
defaultOp.Header = op.Header
defaultOp.Body = op.Body
defaultOp.Timeout = op.Timeout
defaultOp := NewDefaultOptions()
if op.Method != "" {
defaultOp.Method = op.Method
}
if op.Header != nil {
defaultOp.Header = op.Header
}
if op.Body != nil {
defaultOp.Body = op.Body
}
if int(op.Timeout) != 0 {
defaultOp.Timeout = op.Timeout
}

// create a new http client
client := &http.Client{
Expand Down
33 changes: 33 additions & 0 deletions fetch_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package fetch

import (
"reflect"
"testing"
)

func TestFetch(t *testing.T) {
type args struct {
url string
op Options
}
tests := []struct {
name string
args args
want Resp
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := Fetch(tt.args.url, tt.args.op)
if (err != nil) != tt.wantErr {
t.Errorf("Fetch() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Fetch() = %v, want %v", got, tt.want)
}
})
}
}
3 changes: 2 additions & 1 deletion option.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ type Options struct {
Timeout time.Duration
}

func newDefaultOptions() Options {
// NewDefaultOptions create a default options
func NewDefaultOptions() Options {
return Options{
Method: "GET",
Header: map[string]string{
Expand Down

0 comments on commit 87ba938

Please sign in to comment.