Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Max Chechel committed Mar 3, 2018
1 parent 83dc557 commit 6f98081
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 9 deletions.
17 changes: 16 additions & 1 deletion options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestGetOptions(t *testing.T) {
},
},
{
name: "invalid target",
name: "no target",
args: func(t *testing.T) args {
return args{
arguments: []string{""},
Expand All @@ -50,6 +50,21 @@ func TestGetOptions(t *testing.T) {
}
},
},
{
name: "bad target",
args: func(t *testing.T) args {
return args{
arguments: []string{"-t", "!"},
stderr: ioutil.Discard,
exit: func(code int) {
if code != 2 {
t.Errorf("unexpected exit code, got: %d, want: 2", code)
}
t.Skip()
},
}
},
},
{
name: "success",
args: func(t *testing.T) args {
Expand Down
100 changes: 92 additions & 8 deletions tripper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"reflect"
"strings"
"testing"
Expand Down Expand Up @@ -36,8 +37,7 @@ func Test_nopTripper_RoundTrip(t *testing.T) {
name: "failed to dump request",
init: func(*testing.T) nopTripper { return nopTripper{} },
args: func(*testing.T) args {
r, _ := http.NewRequest("POST", "http://github.com/hexdigest/gmeter", errorReader{})
return args{r}
return args{r: httptest.NewRequest("POST", "http://github.com/hexdigest/gmeter", errorReader{})}
},
wantErr: true,
inspectErr: func(err error, t *testing.T) {
Expand All @@ -50,8 +50,7 @@ func Test_nopTripper_RoundTrip(t *testing.T) {
name: "track not found",
init: func(*testing.T) nopTripper { return nopTripper{} },
args: func(*testing.T) args {
r, _ := http.NewRequest("POST", "http://github.com/hexdigest/gmeter", strings.NewReader(""))
return args{r}
return args{r: httptest.NewRequest("POST", "http://github.com/hexdigest/gmeter", strings.NewReader(""))}
},
wantErr: true,
inspectErr: func(err error, t *testing.T) {
Expand Down Expand Up @@ -123,9 +122,8 @@ func Test_RoundTripper_Record(t *testing.T) {
return &RoundTripper{logger: log.New(ioutil.Discard, "", 0)}
},
args: func(t *testing.T) args {
r, _ := http.NewRequest("POST", "https://github.com/hexdigest/gmeter", strings.NewReader("{"))
return args{
r: r,
r: httptest.NewRequest("POST", "https://github.com/hexdigest/gmeter", strings.NewReader("{")),
w: newCheckStatusWriter(t, 400),
}
},
Expand All @@ -137,8 +135,7 @@ func Test_RoundTripper_Record(t *testing.T) {
},
args: func(t *testing.T) args {
body := strings.NewReader(`{"cassette": "nice music"}`)
r, _ := http.NewRequest("POST", "https://github.com/hexdigest/gmeter", body)
return args{r: r}
return args{r: httptest.NewRequest("POST", "https://github.com/hexdigest/gmeter", body)}
},
},
}
Expand Down Expand Up @@ -265,3 +262,90 @@ func Test_RoundTripper_Play(t *testing.T) {
})
}
}

func TestNewRoundTripper(t *testing.T) {
expected := RoundTripper{}
rt := NewRoundTripper(Options{}, nil)
if *rt != expected {
t.Errorf("expected pointer to empty RoundTripper, got: %v", *rt)
}
}

type roundTripperMock struct {
resp *http.Response
err error
}

func (rt roundTripperMock) RoundTrip(*http.Request) (*http.Response, error) {
return rt.resp, rt.err
}

func TestRoundTripper_RoundTrip(t *testing.T) {
type args struct {
r *http.Request
}
tests := []struct {
name string
init func(t *testing.T) *RoundTripper
inspect func(r *RoundTripper, t *testing.T) //inspects receiver after test run

args func(t *testing.T) args

want1 *http.Response
wantErr bool
inspectErr func(err error, t *testing.T) //use for more precise error evaluation after test
}{
{
name: "not initialized",
init: func(t *testing.T) *RoundTripper { return &RoundTripper{} },
args: func(t *testing.T) args {
return args{r: httptest.NewRequest("POST", "http://github.com/hexdigest/gmeter", strings.NewReader(""))}
},
wantErr: true,
inspectErr: func(err error, t *testing.T) {
if err != errNotInitialized {
t.Errorf("unexpected error: %v", err)
}
},
},
{
name: "success",
init: func(t *testing.T) *RoundTripper {
rtMock := roundTripperMock{resp: &http.Response{StatusCode: http.StatusTeapot}, err: nil}
return &RoundTripper{
RoundTripper: rtMock,
logger: log.New(ioutil.Discard, "", 0),
}
},
args: func(t *testing.T) args {
return args{r: httptest.NewRequest("POST", "http://github.com/hexdigest/gmeter", strings.NewReader(""))}
},
want1: &http.Response{StatusCode: http.StatusTeapot},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tArgs := tt.args(t)

receiver := tt.init(t)
got1, err := receiver.RoundTrip(tArgs.r)

if tt.inspect != nil {
tt.inspect(receiver, t)
}

if !reflect.DeepEqual(got1, tt.want1) {
t.Errorf("RoundTripper.RoundTrip got1 = %v, want1: %v", got1, tt.want1)
}

if (err != nil) != tt.wantErr {
t.Fatalf("RoundTripper.RoundTrip error = %v, wantErr: %t", err, tt.wantErr)
}

if tt.inspectErr != nil {
tt.inspectErr(err, t)
}
})
}
}

0 comments on commit 6f98081

Please sign in to comment.