github.com/ghodss/etcd@v0.3.1-0.20140417172404-cc329bfa55cb/http/query_params_test.go (about)

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