Simplify test cases

This commit is contained in:
Roberto Abdelkader Martínez Pérez
2019-10-04 14:08:22 +02:00
parent 6b34047d75
commit bf88e6a78b
+29 -44
View File
@@ -1,63 +1,48 @@
package client package client
import ( import (
"bufio"
"bytes" "bytes"
"net/http" "net/http"
"os"
"testing" "testing"
gock "gopkg.in/h2non/gock.v1" gock "gopkg.in/h2non/gock.v1"
) )
func TestGetInvalidUrl(t *testing.T) { func TestWriteContentToWriter(t *testing.T) {
err := GetData("", "", "", os.Stdout)
if err == nil {
t.Error("Expected error with invalid url ''")
}
}
func TestGetInvalidWriter(t *testing.T) {
err := GetData("http://localhost:8081", "0000", "/", nil)
if err == nil {
t.Error("Expected error with no writer")
}
}
func TestGetURLNotFoundWithUnknownID(t *testing.T) {
defer gock.Off() defer gock.Off()
gock.New("http://localhost").
Get("/handlers/THIS-IS-THE-HANDLER-ID/request/body").
Reply(http.StatusOK).
Body(bytes.NewReader([]byte("FOO")))
gock.New("http://localhost:8081"). var b bytes.Buffer
Get("/handlers/000/").Reply(http.StatusNotFound) buf := bufio.NewWriter(&b)
err := GetData("http://localhost", "THIS-IS-THE-HANDLER-ID", "/request/body", buf)
err := GetData("http://localhost:8081", "000", "/", os.Stdout)
if err == nil {
t.Errorf("Expect not found error but get no error")
}
if gock.IsDone() == false {
t.Error("No expected endpoint called")
}
}
func TestGetRetrieveRequestMethod(t *testing.T) {
defer gock.Off()
gock.New("http://localhost:8081").
Get("/handlers/000/request/method").
Reply(http.StatusAccepted).
BodyString("POST")
rw := new(bytes.Buffer)
err := GetData("http://localhost:8081", "000", "/request/method", rw)
if err != nil { if err != nil {
t.Errorf("Unexpected error %v", err) t.Errorf("Unexpected error: %q", err)
} }
strRes := rw.String()
if strRes != "POST" { if !bytes.Equal(b.Bytes(), []byte("FOO")) {
t.Errorf("POST string expected but found: '%v'", strRes) t.Errorf("Received content mismatch: %q != %q", b.Bytes(), []byte("FOO"))
}
if gock.IsDone() == false {
t.Error("No expected endpoint called")
}
}
func TestPropagateHTTPError(t *testing.T) {
defer gock.Off()
gock.New("http://localhost").
Get("/handlers/THIS-IS-THE-HANDLER-ID/request/body").
Reply(http.StatusTeapot)
err := GetData("http://localhost", "THIS-IS-THE-HANDLER-ID", "/request/body", nil)
if err == nil {
t.Errorf("Expected error not returned")
} }
if gock.IsDone() == false { if gock.IsDone() == false {