| 1 | package g8 |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net/http" |
| 6 | "net/http/httptest" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | var handler http.Handler = &testHandler{} |
| 11 | |
| 12 | func BenchmarkTestHandler(b *testing.B) { |
| 13 | request, _ := http.NewRequest("GET", "/handle", nil) |
| 14 | |
| 15 | router := http.NewServeMux() |
| 16 | router.Handle("/handle", handler) |
| 17 | |
| 18 | for n := 0; n < b.N; n++ { |
| 19 | responseRecorder := httptest.NewRecorder() |
| 20 | router.ServeHTTP(responseRecorder, request) |
| 21 | if responseRecorder.Code != http.StatusOK { |
| 22 | b.Fatalf("%s %s should have returned %d, but returned %d instead", request.Method, request.URL, http.StatusOK, responseRecorder.Code) |
| 23 | } |
| 24 | } |
| 25 | b.ReportAllocs() |
| 26 | } |
| 27 | |
| 28 | func BenchmarkGate_ProtectWhenNoAuthorizationHeader(b *testing.B) { |
| 29 | gate := New().WithAuthorizationService(NewAuthorizationService().WithToken("good-token")) |
| 30 | request, _ := http.NewRequest("GET", "/handle", nil) |
| 31 | |
| 32 | router := http.NewServeMux() |
| 33 | router.Handle("/handle", gate.Protect(handler)) |
| 34 | |
| 35 | for n := 0; n < b.N; n++ { |
| 36 | responseRecorder := httptest.NewRecorder() |
| 37 | router.ServeHTTP(responseRecorder, request) |
| 38 | if responseRecorder.Code != http.StatusUnauthorized { |
| 39 | b.Fatalf("%s %s should have returned %d, but returned %d instead", request.Method, request.URL, http.StatusUnauthorized, responseRecorder.Code) |
| 40 | } |
| 41 | } |
| 42 | b.ReportAllocs() |
| 43 | } |
| 44 | |
| 45 | func BenchmarkGate_ProtectWithInvalidToken(b *testing.B) { |
| 46 | gate := New().WithAuthorizationService(NewAuthorizationService().WithToken("good-token")) |
| 47 | request, _ := http.NewRequest("GET", "/handle", nil) |
| 48 | request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", "bad-token")) |
| 49 | |
| 50 | router := http.NewServeMux() |
| 51 | router.Handle("/handle", gate.Protect(handler)) |
| 52 | |
| 53 | for n := 0; n < b.N; n++ { |
| 54 | responseRecorder := httptest.NewRecorder() |
| 55 | router.ServeHTTP(responseRecorder, request) |
| 56 | if responseRecorder.Code != http.StatusUnauthorized { |
| 57 | b.Fatalf("%s %s should have returned %d, but returned %d instead", request.Method, request.URL, http.StatusUnauthorized, responseRecorder.Code) |
| 58 | } |
| 59 | } |
| 60 | b.ReportAllocs() |
| 61 | } |
| 62 | |
| 63 | func BenchmarkGate_ProtectWithValidToken(b *testing.B) { |
| 64 | gate := New().WithAuthorizationService(NewAuthorizationService().WithToken("good-token")) |
| 65 | request, _ := http.NewRequest("GET", "/handle", http.NoBody) |
| 66 | request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", "good-token")) |
| 67 | |
| 68 | router := http.NewServeMux() |
| 69 | router.Handle("/handle", gate.Protect(handler)) |
| 70 | |
| 71 | for n := 0; n < b.N; n++ { |
| 72 | responseRecorder := httptest.NewRecorder() |
| 73 | router.ServeHTTP(responseRecorder, request) |
| 74 | if responseRecorder.Code != http.StatusOK { |
| 75 | b.Fatalf("%s %s should have returned %d, but returned %d instead", request.Method, request.URL, http.StatusOK, responseRecorder.Code) |
| 76 | } |
| 77 | } |
| 78 | b.ReportAllocs() |
| 79 | } |
| 80 | |
| 81 | func BenchmarkGate_ProtectWithPermissionsAndValidToken(b *testing.B) { |
| 82 | gate := New().WithAuthorizationService(NewAuthorizationService().WithClient(NewClient("token").WithPermission("admin"))) |
| 83 | request, _ := http.NewRequest("GET", "/handle", http.NoBody) |
| 84 | request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", "token")) |
| 85 | |
| 86 | router := http.NewServeMux() |
| 87 | router.Handle("/handle", gate.ProtectWithPermissions(handler, []string{"admin"})) |
| 88 | |
| 89 | for n := 0; n < b.N; n++ { |
| 90 | responseRecorder := httptest.NewRecorder() |
| 91 | router.ServeHTTP(responseRecorder, request) |
| 92 | if responseRecorder.Code != http.StatusOK { |
| 93 | b.Fatalf("%s %s should have returned %d, but returned %d instead", request.Method, request.URL, http.StatusOK, responseRecorder.Code) |
| 94 | } |
| 95 | } |
| 96 | b.ReportAllocs() |
| 97 | } |
| 98 | |
| 99 | func BenchmarkGate_ProtectWithPermissionsAndValidTokenButInsufficientPermissions(b *testing.B) { |
| 100 | gate := New().WithAuthorizationService(NewAuthorizationService().WithClient(NewClient("token").WithPermission("mod"))) |
| 101 | request, _ := http.NewRequest("GET", "/handle", http.NoBody) |
| 102 | request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", "token")) |
| 103 | |
| 104 | router := http.NewServeMux() |
| 105 | router.Handle("/handle", gate.ProtectWithPermissions(handler, []string{"admin"})) |
| 106 | |
| 107 | for n := 0; n < b.N; n++ { |
| 108 | responseRecorder := httptest.NewRecorder() |
| 109 | router.ServeHTTP(responseRecorder, request) |
| 110 | if responseRecorder.Code != http.StatusUnauthorized { |
| 111 | b.Fatalf("%s %s should have returned %d, but returned %d instead", request.Method, request.URL, http.StatusUnauthorized, responseRecorder.Code) |
| 112 | } |
| 113 | } |
| 114 | b.ReportAllocs() |
| 115 | } |
| 116 | |
| 117 | func BenchmarkGate_ProtectConcurrently(b *testing.B) { |
| 118 | gate := New().WithAuthorizationService(NewAuthorizationService().WithToken("good-token")) |
| 119 | |
| 120 | request, _ := http.NewRequest("GET", "/handle", http.NoBody) |
| 121 | request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", "good-token")) |
| 122 | |
| 123 | badRequest, _ := http.NewRequest("GET", "/handle", http.NoBody) |
| 124 | badRequest.Header.Add("Authorization", fmt.Sprintf("Bearer %s", "bad-token")) |
| 125 | |
| 126 | router := http.NewServeMux() |
| 127 | router.Handle("/handle", gate.Protect(handler)) |
| 128 | |
| 129 | b.RunParallel(func(pb *testing.PB) { |
| 130 | for pb.Next() { |
| 131 | responseRecorder := httptest.NewRecorder() |
| 132 | router.ServeHTTP(responseRecorder, request) |
| 133 | if responseRecorder.Code != http.StatusOK { |
| 134 | b.Fatalf("%s %s should have returned %d, but returned %d instead", request.Method, request.URL, http.StatusOK, responseRecorder.Code) |
| 135 | } |
| 136 | responseRecorder = httptest.NewRecorder() |
| 137 | router.ServeHTTP(responseRecorder, badRequest) |
| 138 | if responseRecorder.Code != http.StatusUnauthorized { |
| 139 | b.Fatalf("%s %s should have returned %d, but returned %d instead", badRequest.Method, badRequest.URL, http.StatusUnauthorized, responseRecorder.Code) |
| 140 | } |
| 141 | } |
| 142 | }) |
| 143 | b.ReportAllocs() |
| 144 | } |
| 145 | |
| 146 | func BenchmarkGate_ProtectWithClientProviderConcurrently(b *testing.B) { |
| 147 | gate := New().WithAuthorizationService(NewAuthorizationService().WithClientProvider(mockClientProvider)) |
| 148 | |
| 149 | request, _ := http.NewRequest("GET", "/handle", http.NoBody) |
| 150 | request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", TestProviderClientToken)) |
| 151 | |
| 152 | firstBadRequest, _ := http.NewRequest("GET", "/handle", http.NoBody) |
| 153 | firstBadRequest.Header.Set("Authorization", fmt.Sprintf("Bearer %s", "bad-token-1")) |
| 154 | |
| 155 | secondBadRequest, _ := http.NewRequest("GET", "/handle", http.NoBody) |
| 156 | secondBadRequest.Header.Set("Authorization", fmt.Sprintf("Bearer %s", "bad-token-2")) |
| 157 | |
| 158 | router := http.NewServeMux() |
| 159 | router.Handle("/handle", gate.Protect(handler)) |
| 160 | |
| 161 | b.RunParallel(func(pb *testing.PB) { |
| 162 | for pb.Next() { |
| 163 | responseRecorder := httptest.NewRecorder() |
| 164 | router.ServeHTTP(responseRecorder, request) |
| 165 | if responseRecorder.Code != http.StatusOK { |
| 166 | b.Fatalf("%s %s should have returned %d, but returned %d instead", request.Method, request.URL, http.StatusOK, responseRecorder.Code) |
| 167 | } |
| 168 | responseRecorder = httptest.NewRecorder() |
| 169 | router.ServeHTTP(responseRecorder, firstBadRequest) |
| 170 | if responseRecorder.Code != http.StatusUnauthorized { |
| 171 | b.Fatalf("%s %s should have returned %d, but returned %d instead", firstBadRequest.Method, firstBadRequest.URL, http.StatusUnauthorized, responseRecorder.Code) |
| 172 | } |
| 173 | responseRecorder = httptest.NewRecorder() |
| 174 | router.ServeHTTP(responseRecorder, secondBadRequest) |
| 175 | if responseRecorder.Code != http.StatusUnauthorized { |
| 176 | b.Fatalf("%s %s should have returned %d, but returned %d instead", secondBadRequest.Method, secondBadRequest.URL, http.StatusUnauthorized, responseRecorder.Code) |
| 177 | } |
| 178 | } |
| 179 | }) |
| 180 | b.ReportAllocs() |
| 181 | } |
| 182 | |
| 183 | func BenchmarkGate_ProtectWithValidTokenAndCustomTokenExtractorFuncConcurrently(b *testing.B) { |
| 184 | customTokenExtractorFunc := func(request *http.Request) string { |
| 185 | sessionCookie, err := request.Cookie("session") |
| 186 | if err != nil { |
| 187 | return "" |
| 188 | } |
| 189 | return sessionCookie.Value |
| 190 | } |
| 191 | gate := New().WithAuthorizationService(NewAuthorizationService().WithToken("good-token")).WithCustomTokenExtractor(customTokenExtractorFunc) |
| 192 | request, _ := http.NewRequest("GET", "/handle", http.NoBody) |
| 193 | request.AddCookie(&http.Cookie{Name: "session", Value: "good-token"}) |
| 194 | |
| 195 | router := http.NewServeMux() |
| 196 | router.Handle("/handle", gate.Protect(handler)) |
| 197 | |
| 198 | b.RunParallel(func(pb *testing.PB) { |
| 199 | for pb.Next() { |
| 200 | responseRecorder := httptest.NewRecorder() |
| 201 | router.ServeHTTP(responseRecorder, request) |
| 202 | if responseRecorder.Code != http.StatusOK { |
| 203 | b.Fatalf("%s %s should have returned %d, but returned %d instead", request.Method, request.URL, http.StatusOK, responseRecorder.Code) |
| 204 | } |
| 205 | } |
| 206 | }) |
| 207 | b.ReportAllocs() |
| 208 | } |