-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2194 from brave-intl/master
production 2023-11-01_1
- Loading branch information
Showing
4 changed files
with
85 additions
and
10 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,31 @@ | ||
package middleware | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/brave-intl/bat-go/libs/handlers" | ||
) | ||
|
||
var ( | ||
errUpgradeRequired = errors.New("upgrade required, cutoff exceeded") | ||
) | ||
|
||
// NewUpgradeRequiredByMiddleware passes a service into the context | ||
func NewUpgradeRequiredByMiddleware(cutoff time.Time) func(http.Handler) http.Handler { | ||
return func(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
if time.Now().Before(cutoff) { | ||
next.ServeHTTP(w, r) | ||
return | ||
} | ||
ae := handlers.AppError{ | ||
Cause: errUpgradeRequired, | ||
Message: "upgrade required, cutoff exceeded", | ||
Code: http.StatusUpgradeRequired, | ||
} | ||
ae.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,26 @@ | ||
package middleware | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestUpgradeRequiredByMiddleware(t *testing.T) { | ||
// after cutoff | ||
wrappedHandler := NewUpgradeRequiredByMiddleware(time.Now().Add(-1 * time.Second))(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})) | ||
server := httptest.NewServer(wrappedHandler) | ||
defer server.Close() | ||
resp, _ := http.Get(server.URL) | ||
assert.Equal(t, resp.StatusCode, http.StatusUpgradeRequired, "status code should be upgrade required") | ||
|
||
// not yet cutoff | ||
wrappedHandler = NewUpgradeRequiredByMiddleware(time.Now().Add(1 * time.Second))(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})) | ||
server = httptest.NewServer(wrappedHandler) | ||
defer server.Close() | ||
resp, _ = http.Get(server.URL) | ||
assert.Equal(t, resp.StatusCode, http.StatusOK, "status code should be OK") | ||
} |
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