示例#1
0
文件: uri_test.go 项目: kelyar/clio
func TestSplitPathWithEmptyString(t *testing.T) {
	abs, params := helpers.SplitPath("")
	if abs != "" {
		t.Error("")
	}

	if params != "" {
		t.Error("")
	}
}
示例#2
0
文件: uri_test.go 项目: kelyar/clio
func TestSplitPath(t *testing.T) {
	abs, params := helpers.SplitPath("/a/b/c?a=b&c=d")
	if abs != "/a/b/c" {
		t.Error("")
	}

	if params != "a=b&c=d" {
		t.Error("")
	}
}
示例#3
0
文件: clio.go 项目: kelyar/clio
/**
 *  Finding correct handler to certain method:path
 */
func Router(w http.ResponseWriter, req *http.Request) {

	// splitting whole path into parts
	path, paramsString := helpers.SplitPath(req.URL.String())

	routeFound := false

	method := req.Method
	if IsWebsocket() {
		method = "WS"
	}

	// finding correct handler
	for rawPattern, _ := range routes[method] {
		pattern := helpers.PreparePattern(rawPattern)

		if pattern.MatchString(path) {
			routeFound = true

			// homage to Sinatra's splat
			splat = pattern.FindAllStringSubmatch(path, 100)[0][1:]

			// filling params
			params = helpers.ParseParams(paramsString)

			// calling matched handler
			fmt.Fprintln(w, routes[method][rawPattern]())

			// terminal debugging
			if AppSettings["verbose-output"] != nil && AppSettings["verbose-output"].(bool) == true {
				log.Printf("%s %s\n", method, req.URL.String())
			}
			break
		}
	}
	if !routeFound {
		NotFound(w, req)
	}
}
示例#4
0
文件: uri_test.go 项目: kelyar/clio
func TestSplitPathWithoutParams(t *testing.T) {
	_, params := helpers.SplitPath("/a/b/c?")
	if params != "" {
		t.Error("")
	}
}