github.com/gramework/gramework@v1.8.1-0.20231027140105-82555c9057f5/fasthttprouter_cache_test.go (about) 1 // Copyright 2017-present Kirill Danshin and Gramework contributors 2 // Copyright 2019-present Highload LTD (UK CN: 11893420) 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 11 package gramework 12 13 import ( 14 "testing" 15 ) 16 17 func TestRouterCache(t *testing.T) { 18 cache := &cache{ 19 v: make(map[string]*msc, zero), 20 } 21 22 if _, ok := cache.Get(Slash, GET); ok { 23 t.Fatalf("Cache returned ok flag for key that not exists") 24 } 25 26 cache.Put(Slash, new(node), false, GET) 27 28 if n, ok := cache.Get(Slash, GET); !ok || n == nil { 29 t.Fatalf("Cache returned unexpected result: n=[%v], ok=[%v]", n, ok) 30 } 31 } 32 33 // func testBuildReqRes(method, uri string) (*fasthttp.Request, *fasthttp.Response) { 34 // req, res := fasthttp.AcquireRequest(), fasthttp.AcquireResponse() 35 // req.Header.SetMethod(method) 36 // req.SetRequestURI(uri) 37 // return req, res 38 // } 39 40 // func TestAppServe(t *testing.T) { 41 // const uri = "http://test.request" 42 43 // testCases := []func(*App) (func(string, interface{}) *App, string){ 44 // // check GET request 45 // func(app *App) (func(string, interface{}) *App, string) { 46 // return app.GET, GET 47 // }, 48 // // check POST request 49 // func(app *App) (func(string, interface{}) *App, string) { 50 // return app.POST, POST 51 // }, 52 // // check PUT request 53 // func(app *App) (func(string, interface{}) *App, string) { 54 // return app.PUT, PUT 55 // }, 56 // // check PATCH request 57 // func(app *App) (func(string, interface{}) *App, string) { 58 // return app.PATCH, PATCH 59 // }, 60 // // check DELETE request 61 // func(app *App) (func(string, interface{}) *App, string) { 62 // return app.DELETE, DELETE 63 // }, 64 // // check HEAD request 65 // func(app *App) (func(string, interface{}) *App, string) { 66 // return app.HEAD, HEAD 67 // }, 68 // // check OPTIONS request 69 // func(app *App) (func(string, interface{}) *App, string) { 70 // return app.OPTIONS, OPTIONS 71 // }, 72 // } 73 74 // for _, test := range testCases { 75 // var handleOK bool 76 77 // app := New() 78 // ln := fasthttputil.NewInmemoryListener() 79 // c := &fasthttp.Client{ 80 // Dial: func(addr string) (net.Conn, error) { 81 // return ln.Dial() 82 // }, 83 // } 84 85 // reg, method := test(app) 86 87 // go func() { 88 // _ = app.Serve(ln) 89 // }() 90 91 // reg("/", func() { 92 // handleOK = true 93 // }) 94 // err := c.Do(testBuildReqRes(method, uri)) 95 // if err != nil { 96 // t.Fatal(err) 97 // } 98 99 // ln.Close() 100 101 // if !handleOK { 102 // t.Errorf("%s request was not served correctly", method) 103 // } 104 // } 105 // }