MCPcopy Create free account
hub / github.com/kataras/iris / TestRoutingBasic

Function TestRoutingBasic

_examples/routing/basic/main_test.go:19–88  ·  view source on GitHub ↗

Shows a very basic usage of the httptest. The tests are written in a way to be easy to understand, for a more comprehensive testing examples check out the: _examples/routing/main_test.go, _examples/routing/subdomains/www/main_test.go _examples/file-server and e.t.c. Almost every example which covers

(t *testing.T)

Source from the content-addressed store, hash-verified

17// a new feature from you to learn
18// contains a test file as well.
19func TestRoutingBasic(t *testing.T) {
20 expectedUResponse := func(paramName, paramType, paramValue string) string {
21 s := fmt.Sprintf("before %s (%s), current route name: GET/u/{%s:%s}\n", paramName, paramType, paramName, paramType)
22 s += fmt.Sprintf("%s (%s): %s", paramName, paramType, paramValue)
23 return s
24 }
25
26 var (
27 expectedNotFoundResponse = "Custom route for 404 not found http code, here you can render a view, html, json <b>any valid response</b>."
28
29 expectedIndexResponse = "Hello from /"
30 expectedHomeResponse = `Same as app.Handle("GET", "/", [...])`
31
32 expectedUpathResponse = ":string, :int, :uint, :alphabetical and :path in the same path pattern."
33 expectedUStringResponse = expectedUResponse("username", "string", "abcd123")
34 expectedUIntResponse = expectedUResponse("id", "int", "-1")
35 expectedUUintResponse = expectedUResponse("uid", "uint", "42")
36 expectedUAlphabeticalResponse = expectedUResponse("firstname", "alphabetical", "abcd")
37
38 expectedAPIUsersIndexResponse = map[string]interface{}{"user_id": 42}
39
40 expectedAdminIndexResponse = "<h1>Hello from admin/</h1>"
41
42 expectedSubdomainV1IndexResponse = `Version 1 API. go to <a href="/api/users">/api/users</a>`
43 expectedSubdomainV1APIUsersIndexResponse = "All users"
44 expectedSubdomainV1APIUsersIndexWithParamResponse = "user with id: 42"
45
46 expectedSubdomainWildcardIndexResponse = "Subdomain can be anything, now you're here from: any-subdomain-here"
47 )
48
49 app := newApp()
50 e := httptest.New(t, app)
51
52 e.GET("/anotfound").Expect().Status(httptest.StatusNotFound).
53 Body().IsEqual(expectedNotFoundResponse)
54
55 e.GET("/").Expect().Status(httptest.StatusOK).
56 Body().IsEqual(expectedIndexResponse)
57 e.GET("/home").Expect().Status(httptest.StatusOK).
58 Body().IsEqual(expectedHomeResponse)
59
60 e.GET("/u/some/path/here").Expect().Status(httptest.StatusOK).
61 Body().IsEqual(expectedUpathResponse)
62 e.GET("/u/abcd123").Expect().Status(httptest.StatusOK).
63 Body().IsEqual(expectedUStringResponse)
64 e.GET("/u/-1").Expect().Status(httptest.StatusOK).
65 Body().IsEqual(expectedUIntResponse)
66 e.GET("/u/42").Expect().Status(httptest.StatusOK).
67 Body().IsEqual(expectedUUintResponse)
68 e.GET("/u/abcd").Expect().Status(httptest.StatusOK).
69 Body().IsEqual(expectedUAlphabeticalResponse)
70
71 e.GET("/api/users/42").Expect().Status(httptest.StatusOK).
72 JSON().IsEqual(expectedAPIUsersIndexResponse)
73
74 e.GET("/admin").Expect().Status(httptest.StatusOK).
75 Body().IsEqual(expectedAdminIndexResponse)
76

Callers

nothing calls this directly

Calls 6

NewFunction · 0.92
StatusMethod · 0.80
RequestMethod · 0.80
newAppFunction · 0.70
BodyMethod · 0.65
JSONMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…