func TestParseParamKeyWithoutValue(t *testing.T) { paramsString := "a=b&c" parsedParams := helpers.ParseParams(paramsString) if parsedParams["c"] != "" { t.Error("") } }
func TestParseParam(t *testing.T) { paramsString := "a=b&c=d" parsedParams := helpers.ParseParams(paramsString) if len(parsedParams) != 2 { t.Error("") } if parsedParams["a"] != "b" { t.Error("") } if parsedParams["c"] != "d" { t.Error("") } }
/** * 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) } }