2019-07-30 05:04:08 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2019-10-25 00:27:02 +02:00
|
|
|
"github.com/kataras/iris/v12/httptest"
|
2019-07-30 05:04:08 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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,
|
2020-06-07 14:26:06 +02:00
|
|
|
// _examples/routing/subdomains/www/main_test.go
|
2019-07-30 05:04:08 +02:00
|
|
|
// _examples/file-server and e.t.c.
|
|
|
|
// Almost every example which covers
|
|
|
|
// a new feature from you to learn
|
|
|
|
// contains a test file as well.
|
|
|
|
func TestRoutingBasic(t *testing.T) {
|
|
|
|
expectedUResponse := func(paramName, paramType, paramValue string) string {
|
|
|
|
s := fmt.Sprintf("before %s (%s), current route name: GET/u/{%s:%s}\n", paramName, paramType, paramName, paramType)
|
|
|
|
s += fmt.Sprintf("%s (%s): %s", paramName, paramType, paramValue)
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
expectedNotFoundResponse = "Custom route for 404 not found http code, here you can render a view, html, json <b>any valid response</b>."
|
|
|
|
|
|
|
|
expectedIndexResponse = "Hello from /"
|
|
|
|
expectedHomeResponse = `Same as app.Handle("GET", "/", [...])`
|
|
|
|
|
2019-07-30 17:02:02 +02:00
|
|
|
expectedUpathResponse = ":string, :int, :uint, :alphabetical and :path in the same path pattern."
|
2019-07-30 05:04:08 +02:00
|
|
|
expectedUStringResponse = expectedUResponse("username", "string", "abcd123")
|
|
|
|
expectedUIntResponse = expectedUResponse("id", "int", "-1")
|
|
|
|
expectedUUintResponse = expectedUResponse("uid", "uint", "42")
|
|
|
|
expectedUAlphabeticalResponse = expectedUResponse("firstname", "alphabetical", "abcd")
|
|
|
|
|
|
|
|
expectedAPIUsersIndexResponse = map[string]interface{}{"user_id": 42}
|
|
|
|
|
|
|
|
expectedAdminIndexResponse = "<h1>Hello from admin/</h1>"
|
|
|
|
|
|
|
|
expectedSubdomainV1IndexResponse = `Version 1 API. go to <a href="/api/users">/api/users</a>`
|
|
|
|
expectedSubdomainV1APIUsersIndexResponse = "All users"
|
|
|
|
expectedSubdomainV1APIUsersIndexWithParamResponse = "user with id: 42"
|
|
|
|
|
|
|
|
expectedSubdomainWildcardIndexResponse = "Subdomain can be anything, now you're here from: any-subdomain-here"
|
|
|
|
)
|
|
|
|
|
|
|
|
app := newApp()
|
|
|
|
e := httptest.New(t, app)
|
|
|
|
|
|
|
|
e.GET("/anotfound").Expect().Status(httptest.StatusNotFound).
|
|
|
|
Body().Equal(expectedNotFoundResponse)
|
|
|
|
|
|
|
|
e.GET("/").Expect().Status(httptest.StatusOK).
|
|
|
|
Body().Equal(expectedIndexResponse)
|
|
|
|
e.GET("/home").Expect().Status(httptest.StatusOK).
|
|
|
|
Body().Equal(expectedHomeResponse)
|
|
|
|
|
2019-07-30 17:02:02 +02:00
|
|
|
e.GET("/u/some/path/here").Expect().Status(httptest.StatusOK).
|
|
|
|
Body().Equal(expectedUpathResponse)
|
2019-07-30 05:04:08 +02:00
|
|
|
e.GET("/u/abcd123").Expect().Status(httptest.StatusOK).
|
|
|
|
Body().Equal(expectedUStringResponse)
|
|
|
|
e.GET("/u/-1").Expect().Status(httptest.StatusOK).
|
|
|
|
Body().Equal(expectedUIntResponse)
|
|
|
|
e.GET("/u/42").Expect().Status(httptest.StatusOK).
|
|
|
|
Body().Equal(expectedUUintResponse)
|
|
|
|
e.GET("/u/abcd").Expect().Status(httptest.StatusOK).
|
|
|
|
Body().Equal(expectedUAlphabeticalResponse)
|
|
|
|
|
|
|
|
e.GET("/api/users/42").Expect().Status(httptest.StatusOK).
|
|
|
|
JSON().Equal(expectedAPIUsersIndexResponse)
|
|
|
|
|
|
|
|
e.GET("/admin").Expect().Status(httptest.StatusOK).
|
|
|
|
Body().Equal(expectedAdminIndexResponse)
|
|
|
|
|
|
|
|
e.Request("GET", "/").WithURL("http://v1.example.com").Expect().Status(httptest.StatusOK).
|
|
|
|
Body().Equal(expectedSubdomainV1IndexResponse)
|
|
|
|
|
|
|
|
e.Request("GET", "/api/users").WithURL("http://v1.example.com").Expect().Status(httptest.StatusOK).
|
|
|
|
Body().Equal(expectedSubdomainV1APIUsersIndexResponse)
|
|
|
|
|
|
|
|
e.Request("GET", "/api/users/42").WithURL("http://v1.example.com").Expect().Status(httptest.StatusOK).
|
|
|
|
Body().Equal(expectedSubdomainV1APIUsersIndexWithParamResponse)
|
|
|
|
|
|
|
|
e.Request("GET", "/").WithURL("http://any-subdomain-here.example.com").Expect().Status(httptest.StatusOK).
|
|
|
|
Body().Equal(expectedSubdomainWildcardIndexResponse)
|
|
|
|
}
|