예제 #1
0
func TestPostJsonBodyIsNil(t *testing.T) {
	ts := startServer(200, `{"field": "value"}`)
	defer ts.Close()

	resp := &S{}
	err, _ := (&client.Http{}).PostJson(ts.URL, nil, resp)
	test.Error(t,
		test.Nil(err, "error"),
		test.Equal(1, httpCallsCount, "calls to http server"),
		test.Equal("value", resp.Field, "response"),
	)
}
예제 #2
0
func TestGetJson(t *testing.T) {
	ts := startServer(200, `{"field": "value"}`)
	defer ts.Close()

	s := &S{}
	err, code := (&client.Http{}).GetJson(ts.URL, s)

	test.Error(t,
		test.Nil(err, "error"),
		test.Equal(1, httpCallsCount, "calls to http server"),
		test.Equal(200, code, "code"),
		test.Equal("value", s.Field, "response"),
	)
}
예제 #3
0
func TestPostJsonErrorResponse(t *testing.T) {
	ts := startServer(500, `Error response`)
	defer ts.Close()

	resp := &S{}
	s := &S{
		Field: "value",
	}
	err, code := (&client.Http{}).PostJson(ts.URL, s, resp)
	test.Error(t,
		test.NotNil(err, "error"),
		test.Equal(500, code, "response status code"),
		test.Equal(1, httpCallsCount, "calls to http server"),
	)
}
예제 #4
0
func TestPostJsonUrlIsEmpty(t *testing.T) {
	ts := startServer(200, `{"field": "value"}`)
	defer ts.Close()

	err, _ := (&client.Http{}).PostJson("", nil, nil)
	test.Error(t,
		test.NotNil(err, "error"),
		test.Equal(0, httpCallsCount, "calls to http server"),
	)
}
예제 #5
0
func TestGetJsonTargetIsNil(t *testing.T) {
	ts := startServer(200, `{"field": "value"}`)
	defer ts.Close()

	err, _ := (&client.Http{}).GetJson(ts.URL, nil)
	test.Error(t,
		test.NotNil(err, "error"),
		test.Equal(0, httpCallsCount, "calls to http server"),
	)
}
예제 #6
0
func TestGetJsonInvalidJson(t *testing.T) {
	ts := startServer(200, "That's not a json.")
	defer ts.Close()

	err, _ := (&client.Http{}).GetJson(ts.URL, &S{})
	test.Error(t,
		test.NotNil(err, "error"),
		test.Equal(1, httpCallsCount, "calls to http server"),
	)
}
예제 #7
0
func TestGetJsonErrorResponse(t *testing.T) {
	ts := startServer(404, "Not found")
	defer ts.Close()

	err, _ := (&client.Http{}).GetJson(ts.URL, &S{})
	test.Error(t,
		test.NotNil(err, "error"),
		test.Equal(1, httpCallsCount, "calls to http server"),
	)
}
예제 #8
0
func TestPostJsonResponseIsNil(t *testing.T) {
	ts := startServer(200, ``)
	defer ts.Close()

	s := &S{
		Field: "value",
	}
	err, _ := (&client.Http{}).PostJson(ts.URL, s, nil)
	test.Error(t,
		test.Nil(err, "error"),
		test.Equal(1, httpCallsCount, "calls to http server"),
	)
}
예제 #9
0
func ExampleError() {
	var t = new(testing.T)
	test.Error(t,
		//Error message will be: Expected [1] apples but got [6]
		test.Equal(1, 6, "apples"),
		//Error message will be: Expected price to be not equal to 2.99
		test.NotEqual(2.99, 2.99, "price"),
		//Error message will be: Expected error to be nil
		test.Nil(errors.New("ERROR"), "error"),
		//Error message will be: Expected student name to not be nil
		test.NotNil(nil, "student name"),
	)
}
예제 #10
0
func TestError(t *testing.T) {
	var nilPtr *int
	var nilErr error
	notNilErr := errors.New("Err")
	i := 5

	test.Error(t,
		test.Equal(1, 1, "apples"),
		test.NotEqual(1, 2, "oranges"),
		test.NotNil(&i, "i"),
		test.Nil(nilPtr, "pointer"),
		test.Nil(nilErr, "error"),
		test.NotNil(notNilErr, "error"),
	)
}