-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds middleware to insert X-Request-Start unix millis timestamp
- Loading branch information
1 parent
68df02c
commit 8599b24
Showing
3 changed files
with
68 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package server | ||
|
||
import ( | ||
"net/http" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
const ( | ||
requestStartHeader = "X-Request-Start" | ||
) | ||
|
||
type RequestStartMiddleware struct { | ||
next http.Handler | ||
} | ||
|
||
func WithRequestStartMiddleware(next http.Handler) http.Handler { | ||
return &RequestStartMiddleware{ | ||
next: next, | ||
} | ||
} | ||
|
||
func (h *RequestStartMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
if r.Header.Get(requestStartHeader) == "" { | ||
r.Header.Set(requestStartHeader, strconv.FormatInt(time.Now().UnixMilli(), 10)) | ||
} | ||
h.next.ServeHTTP(w, r) | ||
} |
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,38 @@ | ||
package server | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestRequestStartMiddleware_AddsUnixMilliWhenNotPresent(t *testing.T) { | ||
handler := WithRequestStartMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
timestamp := r.Header.Get(requestStartHeader) | ||
assert.NotEmpty(t, timestamp) | ||
})) | ||
|
||
r := httptest.NewRequest("GET", "/", nil) | ||
w := httptest.NewRecorder() | ||
handler.ServeHTTP(w, r) | ||
|
||
assert.Equal(t, http.StatusOK, w.Code) | ||
} | ||
|
||
func TestRequestStartMiddleware_PreservesExistingHeaderWhenPresent(t *testing.T) { | ||
next := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
timestamp := r.Header.Get(requestStartHeader) | ||
assert.Equal(t, "1234", timestamp) | ||
}) | ||
handler := WithRequestStartMiddleware(next) | ||
|
||
r := httptest.NewRequest("GET", "/", nil) | ||
r.Header.Set(requestStartHeader, "1234") | ||
|
||
w := httptest.NewRecorder() | ||
handler.ServeHTTP(w, r) | ||
|
||
assert.Equal(t, http.StatusOK, w.Code) | ||
} |
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