|
@@ -3,7 +3,7 @@ package api
|
|
import (
|
|
import (
|
|
"bytes"
|
|
"bytes"
|
|
"encoding/json"
|
|
"encoding/json"
|
|
- "fmt"
|
|
+ "io/ioutil"
|
|
"net/http"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"testing"
|
|
@@ -11,7 +11,20 @@ import (
|
|
"code.osinet.fr/fgm/kurz/domain"
|
|
"code.osinet.fr/fgm/kurz/domain"
|
|
)
|
|
)
|
|
|
|
|
|
-func setupPost(seed bool) (*httptest.Server, *http.Client) {
|
|
+func jsonBody(t *testing.T, res *http.Response) Short {
|
|
|
|
+ body, err := ioutil.ReadAll(res.Body)
|
|
|
|
+ var actual Short
|
|
|
|
+ err = json.Unmarshal(body, &actual)
|
|
|
|
+ if err != nil {
|
|
|
|
+ t.Logf("Response body is not valid JSON: %s", body)
|
|
|
|
+ t.FailNow()
|
|
|
|
+ }
|
|
|
|
+ return actual
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+func subTest(t *testing.T, seed bool, targetURL string, expectedStatus int, errorMessage string) *http.Response {
|
|
tr := domain.MakeMockTargetRepo(!seed)
|
|
tr := domain.MakeMockTargetRepo(!seed)
|
|
if seed {
|
|
if seed {
|
|
tr.Data[domain.TargetURL{URL: domain.URL(sampleTarget)}] = domain.ShortURL{URL: domain.URL(sampleShort)}
|
|
tr.Data[domain.TargetURL{URL: domain.URL(sampleTarget)}] = domain.ShortURL{URL: domain.URL(sampleShort)}
|
|
@@ -19,72 +32,66 @@ func setupPost(seed bool) (*httptest.Server, *http.Client) {
|
|
domain.RegisterRepositories(domain.MockShortRepo{}, tr)
|
|
domain.RegisterRepositories(domain.MockShortRepo{}, tr)
|
|
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(HandlePostTarget))
|
|
ts := httptest.NewServer(http.HandlerFunc(HandlePostTarget))
|
|
|
|
+ defer ts.Close()
|
|
|
|
|
|
c := ts.Client()
|
|
c := ts.Client()
|
|
c.CheckRedirect = doNotFollowRedirects
|
|
c.CheckRedirect = doNotFollowRedirects
|
|
|
|
|
|
- return ts, c
|
|
+ target, err := json.Marshal(Target{Target: targetURL})
|
|
-}
|
|
|
|
-
|
|
|
|
-func TestHandlePostTargetHappy(t *testing.T) {
|
|
|
|
- ts, c := setupPost(false)
|
|
|
|
- defer ts.Close()
|
|
|
|
-
|
|
|
|
- target, err := json.Marshal(map[string]string{"target": sampleTarget})
|
|
|
|
if err != nil {
|
|
if err != nil {
|
|
- t.Log(err)
|
|
+ panic(err)
|
|
- t.FailNow()
|
|
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
res, err := c.Post(ts.URL, postContentType, bytes.NewReader(target))
|
|
res, err := c.Post(ts.URL, postContentType, bytes.NewReader(target))
|
|
if err != nil {
|
|
if err != nil {
|
|
t.Log(err)
|
|
t.Log(err)
|
|
t.FailNow()
|
|
t.FailNow()
|
|
}
|
|
}
|
|
- if res.StatusCode != http.StatusCreated {
|
|
|
|
- t.Log("Creation of new short for valid target should succeed")
|
|
|
|
- t.FailNow()
|
|
|
|
- }
|
|
|
|
|
|
|
|
-
|
|
+ if res.StatusCode != expectedStatus {
|
|
- res, err = c.Post(ts.URL, postContentType, bytes.NewReader(target))
|
|
+ t.Log(errorMessage)
|
|
- if err != nil {
|
|
|
|
- t.Log(err)
|
|
|
|
t.FailNow()
|
|
t.FailNow()
|
|
}
|
|
}
|
|
- if res.StatusCode != http.StatusConflict {
|
|
+ return res
|
|
- t.Error("Re-creation of existing short should conflict")
|
|
|
|
- }
|
|
|
|
}
|
|
}
|
|
|
|
|
|
-func TestHandlePostTargetSad(t *testing.T) {
|
|
+
|
|
- ts, c := setupPost(true)
|
|
+
|
|
- defer ts.Close()
|
|
+func TestHandleGetShortHappyNew(t *testing.T) {
|
|
|
|
+ res := subTest(t, false, sampleTarget,
|
|
|
|
+ http.StatusCreated, "Creation of new short for valid target should succeed")
|
|
|
|
|
|
- target, err := json.Marshal(map[string]string{"target": sampleTarget + "bis"})
|
|
+
|
|
- if err != nil {
|
|
+ actual := jsonBody(t, res)
|
|
- fmt.Println(err)
|
|
+ if actual.Short != sampleTarget {
|
|
|
|
+ t.Logf("Response short URL is %s, expected %s", actual.Short, sampleTarget)
|
|
t.FailNow()
|
|
t.FailNow()
|
|
}
|
|
}
|
|
|
|
+}
|
|
|
|
|
|
-
|
|
+
|
|
- res, err := c.Post(ts.URL, postContentType, nil)
|
|
+
|
|
- if err != nil {
|
|
+func TestHandleGetShortHappyOld(t *testing.T) {
|
|
- fmt.Println(err)
|
|
+ res := subTest(t, true, sampleTarget,
|
|
- t.FailNow()
|
|
+ http.StatusConflict, "Re-creation of existing short should conflict")
|
|
- }
|
|
|
|
- if res.StatusCode != http.StatusBadRequest {
|
|
|
|
- t.Error("Creation of short for empty target should be a bad request")
|
|
|
|
- }
|
|
|
|
|
|
|
|
-
|
|
+
|
|
- res, err = c.Post(ts.URL, postContentType, bytes.NewReader(target))
|
|
+ actual := jsonBody(t, res)
|
|
- if err != nil {
|
|
+ if actual.Short != sampleShort {
|
|
- fmt.Println(err)
|
|
+ t.Logf("Response short URL is %s, expected %s", actual.Short, sampleShort)
|
|
t.FailNow()
|
|
t.FailNow()
|
|
}
|
|
}
|
|
- if res.StatusCode != http.StatusInternalServerError {
|
|
+}
|
|
- t.Error("Creation of new short for valid target should fail since repository cannot create")
|
|
+
|
|
- }
|
|
+
|
|
|
|
+func TestHandlePostTargetSadEmpty(t *testing.T) {
|
|
|
|
+ subTest(t, true, "",
|
|
|
|
+ http.StatusBadRequest, "Creation of short for empty target should be a bad request")
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+func TestHandlePostTargetSadUncreated(t *testing.T) {
|
|
|
|
+ subTest(t, true, sampleTarget+"bis",
|
|
|
|
+ http.StatusInternalServerError, "Creation of new short for valid target should fail since repository cannot create")
|
|
}
|
|
}
|