Make client.Request more testable, break coupling with RESTClient
Moves polling to a function provided by the RESTClient, not innate to Request. Moves doRequest from RESTClient to Request for clarity.
This commit is contained in:
@@ -19,7 +19,6 @@ package client
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
@@ -100,36 +99,6 @@ func TestValidatesHostParameter(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestDoRequest(t *testing.T) {
|
||||
invalid := "aaaaa"
|
||||
uri, _ := url.Parse("http://localhost")
|
||||
testClients := []testClient{
|
||||
{Request: testRequest{Method: "GET", Path: "good"}, Response: Response{StatusCode: 200}},
|
||||
{Request: testRequest{Method: "GET", Path: "good"}, Response: Response{StatusCode: 201}, Created: true},
|
||||
{Request: testRequest{Method: "GET", Path: "bad%ZZ"}, Error: true},
|
||||
{Request: testRequest{Method: "GET", Path: "error"}, Response: Response{StatusCode: 500}, Error: true},
|
||||
{Request: testRequest{Method: "POST", Path: "faildecode"}, Response: Response{StatusCode: 200, RawBody: &invalid}},
|
||||
{Request: testRequest{Method: "GET", Path: "failread"}, Response: Response{StatusCode: 200, RawBody: &invalid}},
|
||||
{Client: &Client{&RESTClient{baseURL: uri, Codec: testapi.Codec()}}, Request: testRequest{Method: "GET", Path: "nocertificate"}, Error: true},
|
||||
}
|
||||
for _, c := range testClients {
|
||||
client := c.Setup()
|
||||
prefix := *client.baseURL
|
||||
prefix.Path += c.Request.Path
|
||||
request := &http.Request{
|
||||
Method: c.Request.Method,
|
||||
Header: make(http.Header),
|
||||
URL: &prefix,
|
||||
}
|
||||
response, created, err := client.doRequest(request)
|
||||
if c.Created != created {
|
||||
t.Errorf("expected created %f, got %f", c.Created, created)
|
||||
}
|
||||
//t.Logf("dorequest: %#v\n%#v\n%v", request.URL, response, err)
|
||||
c.ValidateRaw(t, response, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDoRequestBearer(t *testing.T) {
|
||||
status := &api.Status{Status: api.StatusWorking}
|
||||
expectedBody, _ := latest.Codec.Encode(status)
|
||||
@@ -139,12 +108,15 @@ func TestDoRequestBearer(t *testing.T) {
|
||||
T: t,
|
||||
}
|
||||
testServer := httptest.NewServer(&fakeHandler)
|
||||
request, _ := http.NewRequest("GET", testServer.URL+"/foo/bar", nil)
|
||||
request, _ := http.NewRequest("GET", testServer.URL, nil)
|
||||
c, err := RESTClientFor(&Config{Host: testServer.URL, BearerToken: "test"})
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
c.doRequest(request)
|
||||
err = c.Get().Do().Error()
|
||||
if err == nil {
|
||||
t.Fatalf("unexpected non-error: %v", err)
|
||||
}
|
||||
if fakeHandler.RequestReceived.Header.Get("Authorization") != "Bearer test" {
|
||||
t.Errorf("Request is missing authorization header: %#v", *request)
|
||||
}
|
||||
@@ -159,18 +131,17 @@ func TestDoRequestAccepted(t *testing.T) {
|
||||
T: t,
|
||||
}
|
||||
testServer := httptest.NewServer(&fakeHandler)
|
||||
request, _ := http.NewRequest("GET", testServer.URL+"/foo/bar", nil)
|
||||
c, err := RESTClientFor(&Config{Host: testServer.URL, Username: "test"})
|
||||
c, err := RESTClientFor(&Config{Host: testServer.URL, Username: "test", Version: testapi.Version()})
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
body, _, err := c.doRequest(request)
|
||||
if fakeHandler.RequestReceived.Header["Authorization"] == nil {
|
||||
t.Errorf("Request is missing authorization header: %#v", *request)
|
||||
}
|
||||
body, err := c.Get().Path("test").Do().Raw()
|
||||
if err == nil {
|
||||
t.Fatalf("Unexpected non-error")
|
||||
}
|
||||
if fakeHandler.RequestReceived.Header["Authorization"] == nil {
|
||||
t.Errorf("Request is missing authorization header: %#v", fakeHandler.RequestReceived)
|
||||
}
|
||||
se, ok := err.(APIStatus)
|
||||
if !ok {
|
||||
t.Fatalf("Unexpected kind of error: %#v", err)
|
||||
@@ -179,9 +150,9 @@ func TestDoRequestAccepted(t *testing.T) {
|
||||
t.Errorf("Unexpected status: %#v %#v", se.Status(), status)
|
||||
}
|
||||
if body != nil {
|
||||
t.Errorf("Expected nil body, but saw: '%s'", body)
|
||||
t.Errorf("Expected nil body, but saw: '%s'", string(body))
|
||||
}
|
||||
fakeHandler.ValidateRequest(t, "/foo/bar", "GET", nil)
|
||||
fakeHandler.ValidateRequest(t, "/"+testapi.Version()+"/test", "GET", nil)
|
||||
}
|
||||
|
||||
func TestDoRequestAcceptedSuccess(t *testing.T) {
|
||||
@@ -193,17 +164,16 @@ func TestDoRequestAcceptedSuccess(t *testing.T) {
|
||||
T: t,
|
||||
}
|
||||
testServer := httptest.NewServer(&fakeHandler)
|
||||
request, _ := http.NewRequest("GET", testServer.URL+"/foo/bar", nil)
|
||||
c, err := RESTClientFor(&Config{Host: testServer.URL, Username: "user", Password: "pass"})
|
||||
c, err := RESTClientFor(&Config{Host: testServer.URL, Username: "user", Password: "pass", Version: testapi.Version()})
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
body, _, err := c.doRequest(request)
|
||||
if fakeHandler.RequestReceived.Header["Authorization"] == nil {
|
||||
t.Errorf("Request is missing authorization header: %#v", *request)
|
||||
}
|
||||
body, err := c.Get().Path("test").Do().Raw()
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error %#v", err)
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if fakeHandler.RequestReceived.Header["Authorization"] == nil {
|
||||
t.Errorf("Request is missing authorization header: %#v", fakeHandler.RequestReceived)
|
||||
}
|
||||
statusOut, err := latest.Codec.Decode(body)
|
||||
if err != nil {
|
||||
@@ -212,7 +182,7 @@ func TestDoRequestAcceptedSuccess(t *testing.T) {
|
||||
if !reflect.DeepEqual(status, statusOut) {
|
||||
t.Errorf("Unexpected mis-match. Expected %#v. Saw %#v", status, statusOut)
|
||||
}
|
||||
fakeHandler.ValidateRequest(t, "/foo/bar", "GET", nil)
|
||||
fakeHandler.ValidateRequest(t, "/"+testapi.Version()+"/test", "GET", nil)
|
||||
}
|
||||
|
||||
func TestDoRequestFailed(t *testing.T) {
|
||||
@@ -224,12 +194,11 @@ func TestDoRequestFailed(t *testing.T) {
|
||||
T: t,
|
||||
}
|
||||
testServer := httptest.NewServer(&fakeHandler)
|
||||
request, _ := http.NewRequest("GET", testServer.URL+"/foo/bar", nil)
|
||||
c, err := RESTClientFor(&Config{Host: testServer.URL})
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
body, _, err := c.doRequest(request)
|
||||
body, err := c.Get().Do().Raw()
|
||||
if err == nil || body != nil {
|
||||
t.Errorf("unexpected non-error: %#v", body)
|
||||
}
|
||||
@@ -252,12 +221,12 @@ func TestDoRequestCreated(t *testing.T) {
|
||||
T: t,
|
||||
}
|
||||
testServer := httptest.NewServer(&fakeHandler)
|
||||
request, _ := http.NewRequest("GET", testServer.URL+"/foo/bar", nil)
|
||||
c, err := RESTClientFor(&Config{Host: testServer.URL, Username: "user", Password: "pass"})
|
||||
c, err := RESTClientFor(&Config{Host: testServer.URL, Username: "user", Password: "pass", Version: testapi.Version()})
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
body, created, err := c.doRequest(request)
|
||||
created := false
|
||||
body, err := c.Get().Path("test").Do().WasCreated(&created).Raw()
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error %#v", err)
|
||||
}
|
||||
@@ -271,5 +240,5 @@ func TestDoRequestCreated(t *testing.T) {
|
||||
if !reflect.DeepEqual(status, statusOut) {
|
||||
t.Errorf("Unexpected mis-match. Expected %#v. Saw %#v", status, statusOut)
|
||||
}
|
||||
fakeHandler.ValidateRequest(t, "/foo/bar", "GET", nil)
|
||||
fakeHandler.ValidateRequest(t, "/"+testapi.Version()+"/test", "GET", nil)
|
||||
}
|
||||
|
Reference in New Issue
Block a user