Skip to content

Commit

Permalink
chore: adds missing tests to increase the coverage (#40)
Browse files Browse the repository at this point in the history
* adds missing tests to increase the coverage

* adds function descriptions

Co-authored-by: Santiago Garcia <[email protected]>
  • Loading branch information
jorbriib and sangarbe authored May 5, 2021
1 parent 40dd099 commit 7273993
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 33 deletions.
12 changes: 6 additions & 6 deletions matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func bySchemas(schemas ...string) (matcher, error) {
}, nil
}

func byHeaders(headers map[string]string) (matcher, error) {
func byHeaders(headers map[string]string) matcher {

return func(r *http.Request) (bool, *node) {
if r == nil || len(headers) > len(r.Header) {
Expand All @@ -61,10 +61,10 @@ func byHeaders(headers map[string]string) (matcher, error) {
}
}
return true, nil
}, nil
}
}

func byQueryParameters(params map[string]string) (matcher, error) {
func byQueryParameters(params map[string]string) matcher {

return func(r *http.Request) (bool, *node) {
if r == nil || len(params) > len(r.URL.Query()) {
Expand All @@ -76,16 +76,16 @@ func byQueryParameters(params map[string]string) (matcher, error) {
}
}
return true, nil
}, nil
}
}

func byCustomMatcher(custom func(r *http.Request) bool) (matcher, error) {
func byCustomMatcher(custom func(r *http.Request) bool) matcher {

return func(r *http.Request) (bool, *node) {
if r == nil {
return false, nil
}

return custom(r), nil
}, nil
}
}
27 changes: 17 additions & 10 deletions matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,21 @@ func Test_byHost_ReturnsErrorWhenMalformedHost(t *testing.T) {
assertNotNil(t, err)
}

func Test_bySchemas_ReturnsErrorWhenInvalidSchemaFormat(t *testing.T) {
s := "htt{"

_, err := bySchemas(s)
assertNotNil(t, err)
}

func Test_byHeaders_ReturnsFalseWhenInsufficientHeaders(t *testing.T) {
req, _ := http.NewRequest("GET", "/", nil)

headers := map[string]string{
"key1": "value1",
}

m, _ := byHeaders(headers)
m := byHeaders(headers)
matches, _ := m(req)
assertFalse(t, matches)
}
Expand All @@ -75,7 +82,7 @@ func Test_byHeaders_ReturnsFalseWhenHeaderDoNotMach(t *testing.T) {
"key2": "value2",
}

m, _ := byHeaders(headers)
m := byHeaders(headers)
matches, _ := m(req)
assertFalse(t, matches)
}
Expand All @@ -89,31 +96,31 @@ func Test_byHeaders_ReturnsTrueWhenHeadersMatch(t *testing.T) {
"key2": "value2",
}

m, _ := byHeaders(headers)
m := byHeaders(headers)
matches, _ := m(req)
assertTrue(t, matches)
}

func Test_byHeaders_ReturnsFalseWhenInsufficientQueryParams(t *testing.T) {
func Test_byQueryParameters_ReturnsFalseWhenInsufficientQueryParams(t *testing.T) {
req, _ := http.NewRequest("GET", "/", nil)

params := map[string]string{
"key1": "value1",
}

m, _ := byQueryParameters(params)
m := byQueryParameters(params)
matches, _ := m(req)
assertFalse(t, matches)
}

func Test_byHeaders_ReturnsFalseWhenQueryParamsDoNotMach(t *testing.T) {
func Test_byQueryParameters_ReturnsFalseWhenQueryParamsDoNotMach(t *testing.T) {
req, _ := http.NewRequest("GET", "/?key1=value1&key2=invalid", nil)
params := map[string]string{
"key1": "value1",
"key2": "value2",
}

m, _ := byQueryParameters(params)
m := byQueryParameters(params)
matches, _ := m(req)
assertFalse(t, matches)
}
Expand All @@ -125,7 +132,7 @@ func Test_byQueryParameters_ReturnsTrueWhenQueryParamsMatch(t *testing.T) {
"key2": "value2",
}

m, _ := byQueryParameters(params)
m := byQueryParameters(params)
matches, _ := m(req)
assertTrue(t, matches)
}
Expand All @@ -141,11 +148,11 @@ func Test_byCustomMatcher_UsesCustomFunction(t *testing.T) {
return false
}

m, _ := byCustomMatcher(matcherTrue)
m := byCustomMatcher(matcherTrue)
matches, _ := m(req)
assertTrue(t, matches)

m, _ = byCustomMatcher(matcherFalse)
m = byCustomMatcher(matcherFalse)
matches, _ = m(req)
assertFalse(t, matches)
}
Expand Down
26 changes: 9 additions & 17 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func buildURLParameters(leaf *node, path string, offset int, paramsCount uint) U
return paramsBag
}

// RouterConfig is a structure to set the router configuration
type RouterConfig struct {
EnableAutoMethodHead bool
EnableAutoMethodOptions bool
Expand Down Expand Up @@ -146,6 +147,7 @@ func (r *Router) As(asName string) *Router {
return r
}

// MatchingOptions is a structure to define a route name and extend the matching options
type MatchingOptions struct {
Name string
Host string
Expand All @@ -155,6 +157,7 @@ type MatchingOptions struct {
Custom func(r *http.Request) bool
}

// NewMatchingOptions returns the MatchingOptions structure
func NewMatchingOptions() MatchingOptions {
return MatchingOptions{
Name: "",
Expand Down Expand Up @@ -211,26 +214,17 @@ func (r *Router) Register(verb, path string, handler http.HandlerFunc, options .
}

if len(options[0].Headers) > 0 {
matcherByHeaders, err := byHeaders(options[0].Headers)
if err != nil {
return err
}
matcherByHeaders := byHeaders(options[0].Headers)
leaf.matchers = append(leaf.matchers, matcherByHeaders)
}

if len(options[0].QueryParams) > 0 {
matcherByQueryParams, err := byQueryParameters(options[0].QueryParams)
if err != nil {
return err
}
matcherByQueryParams := byQueryParameters(options[0].QueryParams)
leaf.matchers = append(leaf.matchers, matcherByQueryParams)
}

if options[0].Custom != nil {
matcherByCustomFunc, err := byCustomMatcher(options[0].Custom)
if err != nil {
return err
}
matcherByCustomFunc := byCustomMatcher(options[0].Custom)
leaf.matchers = append(leaf.matchers, matcherByCustomFunc)
}
}
Expand Down Expand Up @@ -377,21 +371,19 @@ func (r *Router) Prefix(path string, router *Router) error {
return nil
}

// StaticFiles will serve files from a directory under a prefix path
func (r *Router) StaticFiles(prefix, dir string) error {
return r.Register("GET", prefix+"/{name:.*}", func(writer http.ResponseWriter, request *http.Request) {

urlParams := GetURLParameters(request)
name, err := urlParams.GetByName("name")
if err != nil {
writer.WriteHeader(404)
return
}
name, _ := urlParams.GetByName("name")

request.URL.Path = name
http.FileServer(http.Dir(dir)).ServeHTTP(writer, request)
})
}

// Redirect will redirect a path to an url
func (r *Router) Redirect(path, url string, code ...int) error {
return r.Register(http.MethodGet, path, getRedirectHandler(url, code...))
}
Expand Down
Loading

0 comments on commit 7273993

Please sign in to comment.