Skip to content

Commit

Permalink
Add tests (#39)
Browse files Browse the repository at this point in the history
* add fast http settings

* fix default settings

* fix cache set

* fix cache set

* fix default settings

* Removed not need if check

* caching setting

* Moved cacheKey up because it won't compile if under if

* caching setting

* add tests

* add tests

Co-authored-by: Nagy Salem <[email protected]>
  • Loading branch information
abahmed and muhammednagy authored Jun 14, 2020
1 parent cbbfe32 commit c44f047
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
4 changes: 4 additions & 0 deletions cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ func ExampleCache() {
cache.Set([]byte("user5"), 5)
fmt.Println(cache.Get([]byte("user3")))

cache.Set([]byte("user5"), 6)
fmt.Println(cache.Get([]byte("user5")).(int))

cache2 := newCache(0)
cache2.Set([]byte("user1"), 1)
fmt.Println(cache2.Get([]byte("user1")).(int))
Expand All @@ -31,5 +34,6 @@ func ExampleCache() {
// <nil>
// 2
// <nil>
// 6
// 1
}
4 changes: 2 additions & 2 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,9 +400,9 @@ func (gb *gearbox) matchRouteAgainstRegistered(method, path []byte) (handlersCha
trimmedPath := trimPath(path)

// Try to get from cache if it's enabled
cacheKey := ""
cacheKey := make([]byte, 0)
if !gb.settings.DisableCaching {
cacheKey := append(method, trimmedPath...)
cacheKey = append(method, trimmedPath...)
if cacheResult, ok := gb.cache.Get(cacheKey).(*matchParamsResult); ok {
return cacheResult.Handlers, cacheResult.Params
}
Expand Down
3 changes: 2 additions & 1 deletion router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func TestValidateRoutePath(t *testing.T) {
{input: []byte("/user/*"), isErr: false},
{input: []byte("/user/:name"), isErr: false},
{input: []byte("/user/:name/:name"), isErr: true},
{input: []byte("/user/:name?/get"), isErr: true},
}

for _, tt := range tests {
Expand Down Expand Up @@ -294,7 +295,7 @@ func TestConstructRoutingTree(t *testing.T) {
{method: []byte(MethodGet), path: []byte("/books/search/:pattern:([a-z]+)"), handler: emptyHandlersChain},
{method: []byte(MethodGet), path: []byte("/books/search/:pattern"), handler: emptyHandlersChain},
{method: []byte(MethodGet), path: []byte("/books/search/:pattern1/:pattern2/:pattern3"), handler: emptyHandlersChain},
{method: []byte(MethodGet), path: []byte("/books/search/*"), handler: emptyHandlersChain},
{method: []byte(MethodGet), path: []byte("/books//search/*"), handler: emptyHandlersChain},
{method: []byte(MethodGet), path: []byte("/account/:name?"), handler: emptyHandlersChain},
{method: []byte(MethodGet), path: []byte("/profile/:name:([a-z]+)?"), handler: emptyHandlersChain},
{method: []byte(MethodGet), path: []byte("/order/:name1/:name2:([a-z]+)?"), handler: emptyHandlersChain},
Expand Down

0 comments on commit c44f047

Please sign in to comment.