Initial cloud-services repo - gateway service + pkg modules
This commit is contained in:
96
pkg/httpclient/client_test.go
Normal file
96
pkg/httpclient/client_test.go
Normal file
@@ -0,0 +1,96 @@
|
||||
package httpclient
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"fiskerinc.com/modules/httpclient/mock"
|
||||
)
|
||||
|
||||
const (
|
||||
jsonBody = `{"name":"test data"}`
|
||||
TestErrorTemplate = "%s test. Expected '%v'. Got '%v'"
|
||||
testURL = "https://fiskerinc.com"
|
||||
)
|
||||
|
||||
var (
|
||||
httpHeader = http.Header{"content-type": []string{"application/json"}}
|
||||
errServer = errors.New("Error from web server")
|
||||
)
|
||||
|
||||
func TestGetRequest(t *testing.T) {
|
||||
c := mock.Client{
|
||||
DoFunc: func(*http.Request) (*http.Response, error) {
|
||||
return &http.Response{StatusCode: 200}, nil
|
||||
},
|
||||
}
|
||||
|
||||
Client = &c
|
||||
|
||||
_, err := Get(testURL, httpHeader)
|
||||
if err != nil {
|
||||
t.Fatalf(TestErrorTemplate, "TestGetRequest", errors.New("Error from web server"), err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetRequestError(t *testing.T) {
|
||||
c := mock.Client{
|
||||
DoFunc: func(*http.Request) (*http.Response, error) {
|
||||
return nil, errServer
|
||||
},
|
||||
}
|
||||
|
||||
Client = &c
|
||||
|
||||
_, err := Get(testURL, httpHeader)
|
||||
if err == nil {
|
||||
t.Fatalf(TestErrorTemplate, "TestGetRequestError", errServer, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostRequest(t *testing.T) {
|
||||
c := mock.Client{
|
||||
DoFunc: func(*http.Request) (*http.Response, error) {
|
||||
return &http.Response{StatusCode: 200}, nil
|
||||
},
|
||||
}
|
||||
|
||||
Client = &c
|
||||
|
||||
_, err := Post(testURL, jsonBody, httpHeader)
|
||||
if err != nil {
|
||||
t.Fatalf(TestErrorTemplate, "TestPostRequest", nil, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostRequestError(t *testing.T) {
|
||||
c := mock.Client{
|
||||
DoFunc: func(*http.Request) (*http.Response, error) {
|
||||
return nil, errServer
|
||||
},
|
||||
}
|
||||
|
||||
Client = &c
|
||||
|
||||
_, err := Post(testURL, jsonBody, httpHeader)
|
||||
if err == nil {
|
||||
t.Fatalf(TestErrorTemplate, "TestPostRequestError", errServer, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDoRequest(t *testing.T) {
|
||||
Client = &mock.Client{
|
||||
DoFunc: func(*http.Request) (*http.Response, error) {
|
||||
return &http.Response{StatusCode: 200}, nil
|
||||
},
|
||||
}
|
||||
request, _ := http.NewRequest(http.MethodPost, "http://test.com", nil)
|
||||
response, err := Do(request)
|
||||
if err != nil {
|
||||
t.Errorf(TestErrorTemplate, "Response error", "nil", err)
|
||||
}
|
||||
if response.StatusCode != 200 {
|
||||
t.Errorf(TestErrorTemplate, "Response status", 200, response.StatusCode)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user