12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- package api
- import (
- "net/http"
- "net/http/httptest"
- "testing"
- "github.com/gorilla/mux"
- "code.osinet.fr/fgm/kurz/domain"
- )
- func setupGet() (*httptest.Server, *http.Client) {
- router := mux.NewRouter()
- router.HandleFunc("/{short}", HandleGetShort)
- ts := httptest.NewServer(router)
- c := ts.Client()
- c.CheckRedirect = doNotFollowRedirects
- return ts, c
- }
- func TestHandleGetShortHappy(t *testing.T) {
- // Ensure storage contains the expected mapping.
- sr := make(domain.MockShortRepo)
- sr[domain.ShortURL{URL: sampleShort}] = domain.TargetURL{URL: sampleTarget}
- domain.RegisterRepositories(sr, nil)
- ts, c := setupGet()
- defer ts.Close()
- res, err := c.Get(ts.URL + "/" + sampleShort)
- if err != nil {
- t.Error("Getting an existing short URL should not fail")
- }
- res.Body.Close()
- if res.StatusCode != http.StatusTemporaryRedirect {
- t.Errorf("Existing short URL returned %d, expected %d", res.StatusCode, http.StatusTemporaryRedirect)
- }
- }
- func TestHandleGetShortSadNotFound(t *testing.T) {
- // Ensure empty storage.
- sr := make(domain.MockShortRepo)
- domain.RegisterRepositories(sr, nil)
- ts, c := setupGet()
- defer ts.Close()
- res, err := c.Get(ts.URL + "/" + sampleShort)
- if err != nil {
- t.Error("Getting an nonexistent short URL should not fail")
- }
- res.Body.Close()
- if res.StatusCode != http.StatusNotFound {
- t.Errorf("Existing short URL returned %d, expected %d", res.StatusCode, http.StatusNotFound)
- }
- }
|