-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(register): verify hostname (#465)
- Loading branch information
1 parent
e9e4579
commit 21eece0
Showing
5 changed files
with
120 additions
and
1 deletion.
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
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,18 @@ | ||
// Copyright (c) 2023 Target Brands, Inc. All rights reserved. | ||
// | ||
// Use of this source code is governed by the LICENSE file in this repository. | ||
|
||
package middleware | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
// WorkerHostname is a middleware function that attaches the | ||
// worker hostname to the context of every http.Request. | ||
func WorkerHostname(name string) gin.HandlerFunc { | ||
return func(c *gin.Context) { | ||
c.Set("worker-hostname", name) | ||
c.Next() | ||
} | ||
} |
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,46 @@ | ||
// Copyright (c) 2023 Target Brands, Inc. All rights reserved. | ||
// | ||
// Use of this source code is governed by the LICENSE file in this repository. | ||
|
||
package middleware | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
func TestMiddleware_WorkerHostname(t *testing.T) { | ||
// setup types | ||
got := "" | ||
want := "foobar" | ||
|
||
// setup context | ||
gin.SetMode(gin.TestMode) | ||
|
||
resp := httptest.NewRecorder() | ||
context, engine := gin.CreateTestContext(resp) | ||
context.Request, _ = http.NewRequest(http.MethodGet, "/health", nil) | ||
|
||
// setup mock server | ||
engine.Use(WorkerHostname(want)) | ||
engine.GET("/health", func(c *gin.Context) { | ||
got = c.Value("worker-hostname").(string) | ||
|
||
c.Status(http.StatusOK) | ||
}) | ||
|
||
// run test | ||
engine.ServeHTTP(context.Writer, context.Request) | ||
|
||
if resp.Code != http.StatusOK { | ||
t.Errorf("WorkerHostname returned %v, want %v", resp.Code, http.StatusOK) | ||
} | ||
|
||
if !reflect.DeepEqual(got, want) { | ||
t.Errorf("WorkerHostname is %v, want %v", got, want) | ||
} | ||
} |