query_params_test.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package http
  2. import (
  3. "net/http"
  4. "testing"
  5. )
  6. type NilResponseWriter struct{}
  7. func (w NilResponseWriter) Header() http.Header {
  8. return http.Header{}
  9. }
  10. func (w NilResponseWriter) Write(data []byte) (int, error) {
  11. return 0, nil
  12. }
  13. func (w NilResponseWriter) WriteHeader(code int) {
  14. return
  15. }
  16. type FunctionHandler struct {
  17. f func(*http.Request)
  18. }
  19. func (h FunctionHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  20. h.f(r)
  21. }
  22. func TestQueryParamsLowered(t *testing.T) {
  23. assertFunc := func(req *http.Request) {
  24. if len(req.Form["One"]) != 1 || req.Form["One"][0] != "true" {
  25. t.Errorf("Unexpected value for One: %s", req.Form["One"])
  26. } else if len(req.Form["TWO"]) != 1 || req.Form["TWO"][0] != "false" {
  27. t.Errorf("Unexpected value for TWO")
  28. } else if len(req.Form["three"]) != 2 || req.Form["three"][0] != "true" || req.Form["three"][1] != "false" {
  29. t.Errorf("Unexpected value for three")
  30. }
  31. }
  32. assertHdlr := FunctionHandler{assertFunc}
  33. hdlr := NewLowerQueryParamsHandler(assertHdlr)
  34. respWriter := NilResponseWriter{}
  35. req, _ := http.NewRequest("GET", "http://example.com?One=TRUE&TWO=False&three=true&three=FALSE", nil)
  36. hdlr.ServeHTTP(respWriter, req)
  37. }