Skip to content

Instantly share code, notes, and snippets.

@dylanj

dylanj/main.go Secret

Last active October 28, 2016 18:14
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 dylanj/4d3c7d1ea6f99255df823be717e99bcc to your computer and use it in GitHub Desktop.
Save dylanj/4d3c7d1ea6f99255df823be717e99bcc to your computer and use it in GitHub Desktop.
package main
import (
"net/http"
"github.com/pressly/chi"
"github.com/pressly/chi/render"
)
func a(w http.ResponseWriter, r *http.Request) {
render.JSON(w, r, parse("a", r))
}
func b(w http.ResponseWriter, r *http.Request) {
render.JSON(w, r, parse("b", r))
}
func c(w http.ResponseWriter, r *http.Request) {
render.JSON(w, r, parse("c", r))
}
func parse(req string, r *http.Request) map[string]string {
foo := chi.URLParam(r, "foo")
bar := chi.URLParam(r, "bar")
baz := chi.URLParam(r, "baz")
return map[string]string{
"foo": foo,
"bar": bar,
"baz": baz,
"req": req,
}
}
func router() http.Handler {
r := chi.NewRouter()
r.Route("/api/v1", func(r chi.Router) {
r.Route("/files", func(r chi.Router) {
r.Get("/:foo/hello/:bar", a)
r.Get("/:bar/world/:baz", b)
r.Get("/:bar/sups/:baz/:foo", c)
})
})
return r
}
func main() {
r := router()
http.ListenAndServe(":3001", r)
}
package main
import (
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
)
func req(t *testing.T, route string) map[string]string {
s := httptest.NewServer(router())
defer s.Close()
r, _ := http.Get(s.URL + route)
j := make(map[string]string)
body, _ := ioutil.ReadAll(r.Body)
t.Log(route)
t.Logf("%s", body)
json.Unmarshal(body, &j)
return j
}
func TestRoutingBugA(t *testing.T) {
t.Log("/api/v1/files/:foo/hello/:bar")
r := req(t, "/api/v1/files/a/hello/b")
if r["req"] != "a" {
t.Error("incorrect route")
}
if r["foo"] != "a" {
t.Error("foo should be a")
}
if r["bar"] != "b" {
t.Error("bar should be b")
}
}
func TestRoutingBugB(t *testing.T) {
t.Log("/api/v1/files/:bar/world/:baz")
r := req(t, "/api/v1/files/a/world/b")
if r["req"] != "b" {
t.Error("incorrect route")
}
if r["bar"] != "a" {
t.Error("bar should be a, got:", r["bar"])
}
if r["baz"] != "b" {
t.Error("baz should be b, got:", r["baz"])
}
}
func TestRoutingBugC(t *testing.T) {
t.Log("/api/v1/files/:bar/sups/:baz/:foo")
r := req(t, "/api/v1/files/a/sups/b/c")
if r["req"] != "c" {
t.Error("incorrect route")
}
if r["bar"] != "a" {
t.Error("bar should be a, got:", r["bar"])
}
if r["baz"] != "b" {
t.Error("baz should be b, got:", r["baz"])
}
if r["foo"] != "c" {
t.Error("foo should be c, got:", r["foo"])
}
}
> go test -v .
=== RUN TestRoutingBugA
--- PASS: TestRoutingBugA (0.00s)
main_test.go:24: /api/v1/files/:foo/hello/:bar
main_test.go:17: /api/v1/files/a/hello/b
main_test.go:18: {"bar":"b","baz":"","foo":"a","req":"a"}
=== RUN TestRoutingBugB
--- FAIL: TestRoutingBugB (0.00s)
main_test.go:41: /api/v1/files/:bar/world/:baz
main_test.go:17: /api/v1/files/a/world/b
main_test.go:18: {"bar":"","baz":"b","foo":"a","req":"b"}
main_test.go:49: bar should be a, got:
=== RUN TestRoutingBugC
--- FAIL: TestRoutingBugC (0.00s)
main_test.go:58: /api/v1/files/:bar/sups/:baz/:foo
main_test.go:17: /api/v1/files/a/sups/b/c
main_test.go:18: {"bar":"","baz":"b","foo":"a","req":"c"}
main_test.go:66: bar should be a, got:
main_test.go:74: foo should be c, got: a
FAIL
exit status 1
FAIL github.com/zqzca/chibug 0.011s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment