Skip to content

Instantly share code, notes, and snippets.

@quii
Created February 14, 2020 11:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save quii/67b0792b6b471aee7d2a465c385f9263 to your computer and use it in GitHub Desktop.
Save quii/67b0792b6b471aee7d2a465c385f9263 to your computer and use it in GitHub Desktop.
stitcher tests
package main
import (
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestStitcher(t *testing.T) {
t.Run("it gets both websites", func(t *testing.T) {
site1 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "hello")
}))
defer site1.Close()
site2 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "world")
}))
defer site2.Close()
req := httptest.NewRequest(http.MethodGet, "/", nil)
q := req.URL.Query()
q.Add("url", site1.URL)
q.Add("url", site2.URL)
req.URL.RawQuery = q.Encode()
res := httptest.NewRecorder()
Stitcher(res, req)
if res.Code != http.StatusOK {
t.Fatal("not 200", res.Code)
}
if !strings.Contains(res.Body.String(), "hello") {
t.Fatal("didnt get 'hello' in response", res.Body.String())
}
if !strings.Contains(res.Body.String(), "world") {
t.Fatal("didnt get 'world' in response", res.Body.String())
}
})
t.Run("400 when no querystring values", func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/", nil)
res := httptest.NewRecorder()
Stitcher(res, req)
if res.Code != http.StatusBadRequest {
t.Fatal("not 400", res.Code)
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment