-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use uuids for request ids to guarantee uniqueness
- Loading branch information
Showing
2 changed files
with
42 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
package main | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/google/uuid" | ||
) | ||
|
||
func TestRequestIDWellFormed(t *testing.T) { | ||
// This method of testing middleware is cribbed from | ||
// https://stackoverflow.com/questions/51201056/testing-golang-middleware-that-modifies-the-request | ||
nextHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
val := r.Context().Value(contextKeyRequestID).(string) | ||
if uuid.Validate(val) != nil { | ||
t.Errorf("requestID is not a valid uuid! %v", val) | ||
} | ||
}) | ||
|
||
handlerToTest := setRequestID()(nextHandler) | ||
|
||
req := httptest.NewRequest("GET", "http://foo.bar/", nil) | ||
|
||
handlerToTest.ServeHTTP(httptest.NewRecorder(), req) | ||
} |