mirror of
https://github.com/kataras/iris.git
synced 2025-12-18 10:27:06 +00:00
Add notes for the new lead maintainer of the open-source iris project and align with @get-ion/ion by @hiveminded
Former-commit-id: da4f38eb9034daa49446df3ee529423b98f9b331
This commit is contained in:
125
_examples/routing/main_test.go
Normal file
125
_examples/routing/main_test.go
Normal file
@@ -0,0 +1,125 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/kataras/iris/httptest"
|
||||
)
|
||||
|
||||
func calculatePathAndResponse(method, subdomain, path string, paramKeyValue ...string) (string, string) {
|
||||
paramsLen := 0
|
||||
|
||||
if l := len(paramKeyValue); l >= 2 {
|
||||
paramsLen = len(paramKeyValue) / 2
|
||||
}
|
||||
|
||||
paramsInfo := ""
|
||||
if paramsLen > 0 {
|
||||
for i := 0; i < len(paramKeyValue); i++ {
|
||||
paramKey := paramKeyValue[i]
|
||||
i++
|
||||
if i >= len(paramKeyValue) {
|
||||
panic("paramKeyValue should be align with path parameters {} and must be placed in order")
|
||||
}
|
||||
|
||||
paramValue := paramKeyValue[i]
|
||||
paramsInfo += paramKey + " = " + paramValue + "\n"
|
||||
|
||||
beginParam := strings.IndexByte(path, '{')
|
||||
endParam := strings.IndexByte(path, '}')
|
||||
if beginParam == -1 || endParam == -1 {
|
||||
panic("something wrong with parameters, please define them in order")
|
||||
}
|
||||
|
||||
path = path[:beginParam] + paramValue + path[endParam+1:]
|
||||
}
|
||||
}
|
||||
|
||||
return path, paramsInfo + `Info
|
||||
|
||||
Method: ` + method + `
|
||||
Subdomain: ` + subdomain + `
|
||||
Path: ` + path + `
|
||||
Parameters length: ` + strconv.Itoa(paramsLen)
|
||||
}
|
||||
|
||||
type troute struct {
|
||||
method, subdomain, path string
|
||||
status int
|
||||
expectedBody string
|
||||
contentType string
|
||||
}
|
||||
|
||||
func newTroute(method, subdomain, path string, status int, paramKeyValue ...string) troute {
|
||||
finalPath, expectedBody := calculatePathAndResponse(method, subdomain, path, paramKeyValue...)
|
||||
contentType := "text/plain; charset=UTF-8"
|
||||
|
||||
if status == httptest.StatusNotFound {
|
||||
expectedBody = notFoundHTML
|
||||
contentType = "text/html; charset=UTF-8"
|
||||
}
|
||||
|
||||
return troute{
|
||||
contentType: contentType,
|
||||
method: method,
|
||||
subdomain: subdomain,
|
||||
path: finalPath,
|
||||
status: status,
|
||||
expectedBody: expectedBody,
|
||||
}
|
||||
}
|
||||
|
||||
func TestRouting(t *testing.T) {
|
||||
app := newApp()
|
||||
e := httptest.New(t, app)
|
||||
|
||||
var tests = []troute{
|
||||
// GET
|
||||
newTroute("GET", "", "/healthcheck", httptest.StatusOK),
|
||||
newTroute("GET", "", "/games/{gameID}/clans", httptest.StatusOK, "gameID", "42"),
|
||||
newTroute("GET", "", "/games/{gameID}/clans/clan/{clanPublicID}", httptest.StatusOK, "gameID", "42", "clanPublicID", "93"),
|
||||
newTroute("GET", "", "/games/{gameID}/clans/search", httptest.StatusOK, "gameID", "42"),
|
||||
newTroute("GET", "mysubdomain", "/", httptest.StatusOK),
|
||||
// PUT
|
||||
newTroute("PUT", "", "/games/{gameID}/players/{clanPublicID}", httptest.StatusOK, "gameID", "42", "clanPublicID", "93"),
|
||||
newTroute("PUT", "", "/games/{gameID}/clans/clan/{clanPublicID}", httptest.StatusOK, "gameID", "42", "clanPublicID", "93"),
|
||||
// POST
|
||||
newTroute("POST", "", "/games/{gameID}/clans", httptest.StatusOK, "gameID", "42"),
|
||||
newTroute("POST", "", "/games/{gameID}/players", httptest.StatusOK, "gameID", "42"),
|
||||
newTroute("POST", "", "/games/{gameID}/clans/{clanPublicID}/leave", httptest.StatusOK, "gameID", "42", "clanPublicID", "93"),
|
||||
newTroute("POST", "", "/games/{gameID}/clans/{clanPublicID}/memberships/application", httptest.StatusOK, "gameID", "42", "clanPublicID", "93"),
|
||||
newTroute("POST", "", "/games/{gameID}/clans/{clanPublicID}/memberships/application/{action}", httptest.StatusOK, "gameID", "42", "clanPublicID", "93", "action", "somethinghere"),
|
||||
newTroute("POST", "", "/games/{gameID}/clans/{clanPublicID}/memberships/invitation", httptest.StatusOK, "gameID", "42", "clanPublicID", "93"),
|
||||
newTroute("POST", "", "/games/{gameID}/clans/{clanPublicID}/memberships/invitation/{action}", httptest.StatusOK, "gameID", "42", "clanPublicID", "93", "action", "somethinghere"),
|
||||
newTroute("POST", "", "/games/{gameID}/clans/{clanPublicID}/memberships/delete", httptest.StatusOK, "gameID", "42", "clanPublicID", "93"),
|
||||
newTroute("POST", "", "/games/{gameID}/clans/{clanPublicID}/memberships/promote", httptest.StatusOK, "gameID", "42", "clanPublicID", "93"),
|
||||
newTroute("POST", "", "/games/{gameID}/clans/{clanPublicID}/memberships/demote", httptest.StatusOK, "gameID", "42", "clanPublicID", "93"),
|
||||
// POST: / will be tested alone
|
||||
// custom not found
|
||||
newTroute("GET", "", "/notfound", httptest.StatusNotFound),
|
||||
newTroute("POST", "", "/notfound2", httptest.StatusNotFound),
|
||||
newTroute("PUT", "", "/notfound3", httptest.StatusNotFound),
|
||||
newTroute("GET", "mysubdomain", "/notfound42", httptest.StatusNotFound),
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
et := e.Request(tt.method, tt.path)
|
||||
if tt.subdomain != "" {
|
||||
et.WithURL("http://" + tt.subdomain + ".localhost:8080")
|
||||
}
|
||||
et.Expect().Status(tt.status).Body().Equal(tt.expectedBody)
|
||||
}
|
||||
|
||||
// test POST "/" limit data and post data return
|
||||
|
||||
// test with small body
|
||||
e.POST("/").WithBytes([]byte("ok")).Expect().Status(httptest.StatusOK).Body().Equal("ok")
|
||||
// test with equal to max body size limit
|
||||
bsent := make([]byte, maxBodySize, maxBodySize)
|
||||
e.POST("/").WithBytes(bsent).Expect().Status(httptest.StatusOK).Body().Length().Equal(len(bsent))
|
||||
// test with larger body sent and wait for the custom response
|
||||
largerBSent := make([]byte, maxBodySize+1, maxBodySize+1)
|
||||
e.POST("/").WithBytes(largerBSent).Expect().Status(httptest.StatusBadRequest).Body().Equal("http: request body too large")
|
||||
}
|
||||
Reference in New Issue
Block a user