Beispiel #1
0
func TestSimpleGet(t *testing.T) {
	result := make(map[string][]byte)
	inout := make(map[string][]byte)
	inout["/"] = []byte("index")
	inout["/simple/"] = []byte("simple")
	inout["/world"] = []byte("hello world")
	inout["/darko/luketic"] = []byte("hello darko luketic")

	r := mux.New()
	r.Get("/", (*SimpleContext).Index)
	r.Get("/simple/", Simple)
	r.Get("/{name:string}", (*SimpleContext).Hello)
	r.Get("/{first:string}/{last:string}", func(ctx *SimpleContext, first, last string) {
		ctx.Text("hello " + first + " " + last)
	})

	ts := httptest.NewServer(r)
	defer ts.Close()

	for url, expected := range inout {
		rsp, err := http.Get(ts.URL + url)
		if err != nil {
			t.Fatal(err)
		}
		result[url], err = ioutil.ReadAll(rsp.Body)
		if err != nil {
			t.Fatal(err)
		}
		if !bytes.Equal(result[url], expected) {
			t.Errorf("Request GET %s returned %s. Expected %s\n", url, result[url], expected)
		}
	}
}
Beispiel #2
0
func TestCustomContext(t *testing.T) {
	result := make(map[string][]byte)
	post := make(map[string][]byte)

	r := mux.New()
	r.Post("/", (*CustomContext).GetName)

	ts := httptest.NewServer(r)
	defer ts.Close()

	post["/"] = []byte("Hello Darko")

	client := &http.Client{}

	for url, expected := range post {
		buf := bytes.NewBuffer(nil)
		enc := json.NewEncoder(buf)
		data := new(CustomData)
		data.Name = "Darko"
		if err := enc.Encode(data); err != nil {
			t.Fatal(err.Error())
		}
		req, err := http.NewRequest("POST", ts.URL+url, buf)
		if err != nil {
			t.Fatal(err)
		}
		rsp, err := client.Do(req)
		if err != nil {
			t.Fatal(err)
		}
		result[url], err = ioutil.ReadAll(rsp.Body)
		if err != nil {
			t.Fatal(err)
		}
		if !bytes.Equal(result[url], expected) {
			t.Errorf("Request POST %s returned %v. Expected %v\n", url, result[url], expected)
		}
	}

}
Beispiel #3
0
// Method Matching
func TestMethodMatching(t *testing.T) {
	result := make(map[string][]byte)
	get := make(map[string][]byte)
	post := make(map[string][]byte)
	put := make(map[string][]byte)
	del := make(map[string][]byte)
	pat := make(map[string][]byte)

	r := mux.New()
	r.Get("/", func(ctx *context.Context) {
		ctx.Text(ctx.Request.Method)
	})
	r.Get("/{in:string}", func(ctx *context.Context, in string) {
		ctx.Text(ctx.Request.Method + in)
	})
	r.Get("/{in:string}/{in2:string}", func(ctx *context.Context, in, in2 string) {
		ctx.Text(ctx.Request.Method + in + in2)
	})

	r.Post("/", func(ctx *context.Context) {
		ctx.Text(ctx.Request.Method)
	})
	r.Post("/{in:string}", func(ctx *context.Context, in string) {
		ctx.Text(ctx.Request.Method + in)
	})
	r.Post("/{in:string}/{in2:string}", func(ctx *context.Context, in, in2 string) {
		ctx.Text(ctx.Request.Method + in + in2)
	})

	r.Put("/", func(ctx *context.Context) {
		ctx.Text(ctx.Request.Method)
	})
	r.Put("/{in:string}", func(ctx *context.Context, in string) {
		ctx.Text(ctx.Request.Method + in)
	})
	r.Put("/{in:string}/{in2:string}", func(ctx *context.Context, in, in2 string) {
		ctx.Text(ctx.Request.Method + in + in2)
	})

	r.Patch("/", func(ctx *context.Context) {
		ctx.Text(ctx.Request.Method)
	})
	r.Patch("/{in:string}", func(ctx *context.Context, in string) {
		ctx.Text(ctx.Request.Method + in)
	})
	r.Patch("/{in:string}/{in2:string}", func(ctx *context.Context, in, in2 string) {
		ctx.Text(ctx.Request.Method + in + in2)
	})

	r.Delete("/", func(ctx *context.Context) {
		ctx.Text(ctx.Request.Method)
	})
	r.Delete("/{in:string}", func(ctx *context.Context, in string) {
		ctx.Text(ctx.Request.Method + in)
	})
	r.Delete("/{in:string}/{in2:string}", func(ctx *context.Context, in, in2 string) {
		ctx.Text(ctx.Request.Method + in + in2)
	})

	ts := httptest.NewServer(r)
	defer ts.Close()

	get["/"] = []byte("GET")
	get["/a"] = []byte("GETa")
	get["/b"] = []byte("GETb")
	get["/a/a"] = []byte("GETaa")
	get["/a/b"] = []byte("GETab")
	get["/b/a"] = []byte("GETba")
	post["/"] = []byte("POST")
	post["/b"] = []byte("POSTb")
	post["/b/c"] = []byte("POSTbc")
	post["/b/d"] = []byte("POSTbd")
	put["/"] = []byte("PUT")
	put["/c"] = []byte("PUTc")
	put["/c/c"] = []byte("PUTcc")
	put["/c/d"] = []byte("PUTcd")
	put["/c/de"] = []byte("PUTcde")
	del["/"] = []byte("DELETE")
	del["/d"] = []byte("DELETEd")
	del["/d/l"] = []byte("DELETEdl")
	del["/d/a"] = []byte("DELETEda")
	del["/z/d"] = []byte("DELETEzd")
	pat["/"] = []byte("PATCH")
	pat["/e"] = []byte("PATCHe")
	pat["/e/b"] = []byte("PATCHeb")
	pat["/e/u"] = []byte("PATCHeu")

	client := &http.Client{}

	for url, expected := range get {
		req, err := http.NewRequest("GET", ts.URL+url, nil)
		if err != nil {
			t.Fatal(err)
		}
		rsp, err := client.Do(req)
		if err != nil {
			t.Fatal(err)
		}
		result[url], err = ioutil.ReadAll(rsp.Body)
		if err != nil {
			t.Fatal(err)
		}
		if !bytes.Equal(result[url], expected) {
			t.Errorf("Request GET %s returned %v. Expected %v\n", url, result[url], expected)
		}
	}

	for url, expected := range post {
		req, err := http.NewRequest("POST", ts.URL+url, nil)
		if err != nil {
			t.Fatal(err)
		}
		rsp, err := client.Do(req)
		if err != nil {
			t.Fatal(err)
		}
		result[url], err = ioutil.ReadAll(rsp.Body)
		if err != nil {
			t.Fatal(err)
		}
		if !bytes.Equal(result[url], expected) {
			t.Errorf("Request POST %s returned %v. Expected %v\n", url, result[url], expected)
		}
	}

	for url, expected := range put {
		req, err := http.NewRequest("PUT", ts.URL+url, nil)
		if err != nil {
			t.Fatal(err)
		}
		rsp, err := client.Do(req)
		if err != nil {
			t.Fatal(err)
		}
		result[url], err = ioutil.ReadAll(rsp.Body)
		if err != nil {
			t.Fatal(err)
		}
		if !bytes.Equal(result[url], expected) {
			t.Errorf("Request PUT %s returned %v. Expected %v\n", url, result[url], expected)
		}
	}

	for url, expected := range pat {
		req, err := http.NewRequest("PATCH", ts.URL+url, nil)
		if err != nil {
			t.Fatal(err)
		}
		rsp, err := client.Do(req)
		if err != nil {
			t.Fatal(err)
		}
		result[url], err = ioutil.ReadAll(rsp.Body)
		if err != nil {
			t.Fatal(err)
		}
		if !bytes.Equal(result[url], expected) {
			t.Errorf("Request PATCH %s returned %v. Expected %v\n", url, result[url], expected)
		}
	}

	for url, expected := range del {
		req, err := http.NewRequest("DELETE", ts.URL+url, nil)
		if err != nil {
			t.Fatal(err)
		}
		rsp, err := client.Do(req)
		if err != nil {
			t.Fatal(err)
		}
		result[url], err = ioutil.ReadAll(rsp.Body)
		if err != nil {
			t.Fatal(err)
		}
		if !bytes.Equal(result[url], expected) {
			t.Errorf("Request DELETE %s returned %v. Expected %v\n", url, result[url], expected)
		}
	}
}
Beispiel #4
0
func TestParameterMatching(t *testing.T) {
	result := make(map[string][]byte)
	get := make(map[string][]byte)

	r := mux.New()
	r.Get("/no/{in}", func(ctx *context.Context, in string) {
		ctx.Text(in)
	})
	r.Get("/no/{in}/out", func(ctx *context.Context, in string) {
		ctx.Text(in)
	})
	r.Get("/string/{in:string}", func(ctx *context.Context, in string) {
		ctx.Text(in)
	})
	r.Get("/int/{in:int}", func(ctx *context.Context, in string) {
		ctx.Text(in)
	})
	r.Get("/bsonid/{in:bsonid}", func(ctx *context.Context, in string) {
		ctx.Text(in)
	})
	r.Get("/slug/{in:slug}", func(ctx *context.Context, in string) {
		ctx.Text(in)
	})

	ts := httptest.NewServer(r)
	defer ts.Close()

	get["/no/123"] = []byte("123")
	get["/no/das123"] = []byte("das123")
	get["/no/123/out"] = []byte("123")
	get["/no/das123/out"] = []byte("das123")
	get["/string/darko"] = []byte("darko")
	get["/string/123"] = []byte("404 page not found\x0a")
	get["/int/darko"] = []byte("404 page not found\x0a")
	get["/int/123"] = []byte("123")
	get["/bsonid/123"] = []byte("404 page not found\x0a")
	get["/bsonid/01234567890123456789abcd"] = []byte("01234567890123456789abcd")
	get["/bsonid/0a1b2c3d4e5f23456789abcd"] = []byte("0a1b2c3d4e5f23456789abcd")
	get["/bsonid/0123456a89b123d567e9abc"] = []byte("404 page not found\x0a")
	get["/bsonid/0123456a89b123d567e9abcg"] = []byte("404 page not found\x0a")
	get["/slug/"] = []byte("404 page not found\x0a")
	get["/slug/darko"] = []byte("darko")
	get["/slug/darko-"] = []byte("404 page not found\x0a")
	get["/slug/darko-0"] = []byte("darko-0")

	client := &http.Client{}

	for url, expected := range get {
		req, err := http.NewRequest("GET", ts.URL+url, nil)
		if err != nil {
			t.Fatal(err)
		}
		rsp, err := client.Do(req)
		if err != nil {
			t.Fatal(err)
		}
		result[url], err = ioutil.ReadAll(rsp.Body)
		if err != nil {
			t.Fatal(err)
		}
		if !bytes.Equal(result[url], expected) {
			t.Errorf("Request GET %s returned %v. Expected %v\n", url, result[url], expected)
		}
	}

}