post_test.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package api
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "io/ioutil"
  6. "net/http"
  7. "net/http/httptest"
  8. "testing"
  9. "code.osinet.fr/fgm/kurz/domain"
  10. )
  11. func jsonBody(t *testing.T, res *http.Response) Short {
  12. body, err := ioutil.ReadAll(res.Body)
  13. var actual Short
  14. err = json.Unmarshal(body, &actual)
  15. if err != nil {
  16. t.Logf("Response body is not valid JSON: %s", body)
  17. t.FailNow()
  18. }
  19. return actual
  20. }
  21. // subTest performs the main test work: settings up repositories, querying the
  22. // web API, and ensuring response status and format.
  23. func subTest(t *testing.T, seed bool, targetURL string, expectedStatus int, errorMessage string) *http.Response {
  24. tr := domain.MakeMockTargetRepo(!seed)
  25. if seed {
  26. tr.Data[domain.TargetURL{URL: domain.URL(sampleTarget)}] = domain.ShortURL{URL: domain.URL(sampleShort)}
  27. }
  28. domain.RegisterRepositories(domain.MockShortRepo{}, tr)
  29. ts := httptest.NewServer(http.HandlerFunc(handlePostTarget))
  30. defer ts.Close()
  31. c := ts.Client()
  32. c.CheckRedirect = doNotFollowRedirects
  33. target, err := json.Marshal(Target{Target: targetURL})
  34. if err != nil {
  35. panic(err)
  36. }
  37. res, err := c.Post(ts.URL, postContentType, bytes.NewReader(target))
  38. if err != nil {
  39. t.Log(err)
  40. t.FailNow()
  41. }
  42. if res.StatusCode != expectedStatus {
  43. t.Log(errorMessage)
  44. t.FailNow()
  45. }
  46. return res
  47. }
  48. // TestHandleGetShortHappyNew ensures that submitting a new valid target succeeds
  49. // with 201 and returns a new short.
  50. func TestHandleGetShortHappyNew(t *testing.T) {
  51. res := subTest(t, false, sampleTarget,
  52. http.StatusCreated, "Creation of new short for valid target should succeed")
  53. // Mock API creates short URLs equal to the target.
  54. actual := jsonBody(t, res)
  55. if actual.Short != sampleTarget {
  56. t.Logf("Response short URL is %s, expected %s", actual.Short, sampleTarget)
  57. t.FailNow()
  58. }
  59. }
  60. // TestHandleGetShortHappyOld ensures that submitting an existing target fails
  61. // with 409 and returns an existing short.
  62. func TestHandleGetShortHappyOld(t *testing.T) {
  63. res := subTest(t, true, sampleTarget,
  64. http.StatusConflict, "Re-creation of existing short should conflict")
  65. // It was created that way in the previous Post() call.
  66. actual := jsonBody(t, res)
  67. if actual.Short != sampleShort {
  68. t.Logf("Response short URL is %s, expected %s", actual.Short, sampleShort)
  69. t.FailNow()
  70. }
  71. }
  72. // TestHandlePostTargetSadEmpty ensures that submitting an invalid target fails with 400.
  73. func TestHandlePostTargetSadEmpty(t *testing.T) {
  74. subTest(t, true, "",
  75. http.StatusBadRequest, "Creation of short for empty target should be a bad request")
  76. }
  77. // TestHandlePostTargetSadUncreated ensures that submitting a new valid target
  78. // fails with 50x when the repository cannot create new short URLs.
  79. func TestHandlePostTargetSadUncreated(t *testing.T) {
  80. subTest(t, true, sampleTarget+"bis",
  81. http.StatusInternalServerError, "Creation of new short for valid target should fail since repository cannot create")
  82. }