|
@@ -1,7 +1,10 @@
|
|
|
package rest
|
|
|
|
|
|
import (
|
|
|
+ "fmt"
|
|
|
"io/ioutil"
|
|
|
+ "net/http"
|
|
|
+ "net/http/httptest"
|
|
|
"strings"
|
|
|
"testing"
|
|
|
)
|
|
@@ -48,3 +51,36 @@ func TestJSONDecoderBadJSON(t *testing.T) {
|
|
|
t.Errorf("There should be an error on decoding, %v", result)
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+func fakeServer(statusCode int, data string) *httptest.Server {
|
|
|
+ function := func(w http.ResponseWriter, r *http.Request) {
|
|
|
+ w.WriteHeader(statusCode)
|
|
|
+ w.Header().Set("Content-Type", "text")
|
|
|
+ fmt.Fprint(w, data)
|
|
|
+ }
|
|
|
+
|
|
|
+ return httptest.NewServer(http.HandlerFunc(function))
|
|
|
+}
|
|
|
+
|
|
|
+func TestGET(t *testing.T) {
|
|
|
+ expected := "Some text"
|
|
|
+ server := fakeServer(http.StatusOK, expected)
|
|
|
+ defer server.Close()
|
|
|
+
|
|
|
+ data, err := GET(server.URL)
|
|
|
+ defer data.Close()
|
|
|
+ if err != nil {
|
|
|
+ t.Error(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ result, errReading := ioutil.ReadAll(data)
|
|
|
+ if errReading != nil {
|
|
|
+ t.Error(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ resultString := string(result)
|
|
|
+
|
|
|
+ if expected != resultString {
|
|
|
+ t.Errorf("Wrong result, %v", resultString)
|
|
|
+ }
|
|
|
+}
|