Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create v4 #40

Merged
merged 3 commits into from
Feb 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

vendor
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ import (
"os"
"runtime"

"github.com/gol4ng/httpware/v3"
"github.com/gol4ng/httpware/v3/tripperware"
"github.com/gol4ng/httpware/v4"
"github.com/gol4ng/httpware/v4/tripperware"
)

func main(){
Expand All @@ -66,9 +66,9 @@ import (
"os"
"runtime"

"github.com/gol4ng/httpware/v3"
"github.com/gol4ng/httpware/v3/correlation_id"
"github.com/gol4ng/httpware/v3/middleware"
"github.com/gol4ng/httpware/v4"
"github.com/gol4ng/httpware/v4/correlation_id"
"github.com/gol4ng/httpware/v4/middleware"
)

func main(){
Expand Down
12 changes: 8 additions & 4 deletions auth/authenticator.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package auth

import (
"context"
)

type Authenticator interface {
Authenticate(Credential) (Credential, error)
Authenticate(context.Context, Credential) (Credential, error)
}

type AuthenticatorFunc func(Credential) (Credential, error)
type AuthenticatorFunc func(context.Context, Credential) (Credential, error)

func (a AuthenticatorFunc) Authenticate(credential Credential) (Credential, error) {
return a(credential)
func (a AuthenticatorFunc) Authenticate(ctx context.Context, credential Credential) (Credential, error) {
return a(ctx, credential)
}
2 changes: 1 addition & 1 deletion auth/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"net/http"
"testing"

"github.com/gol4ng/httpware/v3/auth"
"github.com/gol4ng/httpware/v4/auth"
"github.com/stretchr/testify/assert"
)

Expand Down
2 changes: 1 addition & 1 deletion correlation_id/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/stretchr/testify/assert"

"github.com/gol4ng/httpware/v3/correlation_id"
"github.com/gol4ng/httpware/v4/correlation_id"
)

func TestNewConfig(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion correlation_id/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/stretchr/testify/assert"

"github.com/gol4ng/httpware/v3/correlation_id"
"github.com/gol4ng/httpware/v4/correlation_id"
)

func Test_Random(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion correlation_id/rand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/stretchr/testify/assert"

"github.com/gol4ng/httpware/v3/correlation_id"
"github.com/gol4ng/httpware/v4/correlation_id"
)

func Test_LockedSource_Int63(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/gol4ng/httpware/v3
module github.com/gol4ng/httpware/v4

go 1.10

Expand Down
2 changes: 1 addition & 1 deletion metrics/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/stretchr/testify/assert"

"github.com/gol4ng/httpware/v3/metrics"
"github.com/gol4ng/httpware/v4/metrics"
)

func TestConfig_Options(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion metrics/prometheus/recorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"time"

"github.com/gol4ng/httpware/v3/metrics"
"github.com/gol4ng/httpware/v4/metrics"

"github.com/prometheus/client_golang/prometheus"
)
Expand Down
13 changes: 7 additions & 6 deletions middleware/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package middleware
import (
"net/http"

"github.com/gol4ng/httpware/v3"
"github.com/gol4ng/httpware/v3/auth"
"github.com/gol4ng/httpware/v4"
"github.com/gol4ng/httpware/v4/auth"
)

// Authentication middleware delegate the authentication process to the AuthenticateFunc
Expand Down Expand Up @@ -74,17 +74,18 @@ func WithSuccessMiddleware(middleware httpware.Middleware) AuthOption {

// NewAuthenticateFunc is an AuthenticateFunc that find, authenticate and hydrate credentials on the request context
func NewAuthenticateFunc(authenticator auth.Authenticator, options ...AuthFuncOption) AuthenticateFunc {
config := newAuthFuncConfig(options...)
config := NewAuthFuncConfig(options...)
return func(request *http.Request) (*http.Request, error) {
ctx := request.Context()
credential := config.credentialFinder(request)
if authenticator != nil {
creds, err := authenticator.Authenticate(credential)
creds, err := authenticator.Authenticate(ctx, credential)
if err != nil {
return request, err
}
credential = creds
}
return request.WithContext(auth.CredentialToContext(request.Context(), credential)), nil
return request.WithContext(auth.CredentialToContext(ctx, credential)), nil
}
}

Expand All @@ -101,7 +102,7 @@ func (o *AuthFuncConfig) apply(options ...AuthFuncOption) {
}
}

func newAuthFuncConfig(options ...AuthFuncOption) *AuthFuncConfig {
func NewAuthFuncConfig(options ...AuthFuncOption) *AuthFuncConfig {
opts := &AuthFuncConfig{
credentialFinder: DefaultCredentialFinder,
}
Expand Down
12 changes: 6 additions & 6 deletions middleware/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"net/http/httptest"
"testing"

"github.com/gol4ng/httpware/v3/auth"
"github.com/gol4ng/httpware/v3/middleware"
"github.com/gol4ng/httpware/v3/mocks"
"github.com/gol4ng/httpware/v4/auth"
"github.com/gol4ng/httpware/v4/middleware"
"github.com/gol4ng/httpware/v4/mocks"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -164,7 +164,7 @@ func TestNewAuthenticateFunc(t *testing.T) {
request.Header.Set("Authorization", "my_credential")

authenticator := &mocks.Authenticator{}
authenticator.On("Authenticate", "my_credential").Return("my_authenticate_credential", nil)
authenticator.On("Authenticate", context.TODO(), "my_credential").Return("my_authenticate_credential", nil)

authenticateFunc := middleware.NewAuthenticateFunc(authenticator)

Expand All @@ -179,7 +179,7 @@ func TestNewAuthenticateFunc_WithCredentialFinder(t *testing.T) {
request := httptest.NewRequest(http.MethodGet, "http://fake-addr", nil)

authenticator := &mocks.Authenticator{}
authenticator.On("Authenticate", "my_credential_finder_value").Return("my_authenticate_credential", nil)
authenticator.On("Authenticate", context.TODO(), "my_credential_finder_value").Return("my_authenticate_credential", nil)

authenticateFunc := middleware.NewAuthenticateFunc(
authenticator,
Expand All @@ -201,7 +201,7 @@ func TestNewAuthenticateFunc_Error(t *testing.T) {

err := errors.New("my_authenticate_error")
authenticator := &mocks.Authenticator{}
authenticator.On("Authenticate", "my_credential").Return("my_authenticate_credential", err)
authenticator.On("Authenticate", context.TODO(), "my_credential").Return("my_authenticate_credential", err)

authenticateFunc := middleware.NewAuthenticateFunc(authenticator)

Expand Down
4 changes: 2 additions & 2 deletions middleware/correlation_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"context"
"net/http"

"github.com/gol4ng/httpware/v3"
"github.com/gol4ng/httpware/v3/correlation_id"
"github.com/gol4ng/httpware/v4"
"github.com/gol4ng/httpware/v4/correlation_id"
)

// CorrelationId middleware get request id header if provided or generate a request id
Expand Down
6 changes: 3 additions & 3 deletions middleware/correlation_id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (

"github.com/stretchr/testify/assert"

"github.com/gol4ng/httpware/v3"
"github.com/gol4ng/httpware/v3/correlation_id"
"github.com/gol4ng/httpware/v3/middleware"
"github.com/gol4ng/httpware/v4"
"github.com/gol4ng/httpware/v4/correlation_id"
"github.com/gol4ng/httpware/v4/middleware"
)

func TestCorrelationId(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion middleware/enable.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package middleware

import (
"github.com/gol4ng/httpware/v3"
"github.com/gol4ng/httpware/v4"
)

// Enable middleware is used to conditionnaly add a middleware to a MiddlewareStack
Expand Down
4 changes: 2 additions & 2 deletions middleware/enable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"net/http/httptest"
"testing"

"github.com/gol4ng/httpware/v3"
"github.com/gol4ng/httpware/v3/middleware"
"github.com/gol4ng/httpware/v4"
"github.com/gol4ng/httpware/v4/middleware"
"github.com/stretchr/testify/assert"
)

Expand Down
4 changes: 2 additions & 2 deletions middleware/interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package middleware
import (
"net/http"

"github.com/gol4ng/httpware/v3"
"github.com/gol4ng/httpware/v3/interceptor"
"github.com/gol4ng/httpware/v4"
"github.com/gol4ng/httpware/v4/interceptor"
)

// Interceptor middleware allow multiple req.Body read and allow to set callback before and after roundtrip
Expand Down
4 changes: 2 additions & 2 deletions middleware/interceptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"net/http/httptest"
"testing"

"github.com/gol4ng/httpware/v3"
"github.com/gol4ng/httpware/v3/middleware"
"github.com/gol4ng/httpware/v4"
"github.com/gol4ng/httpware/v4/middleware"
"github.com/stretchr/testify/assert"
)

Expand Down
4 changes: 2 additions & 2 deletions middleware/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"strconv"
"time"

"github.com/gol4ng/httpware/v3"
"github.com/gol4ng/httpware/v3/metrics"
"github.com/gol4ng/httpware/v4"
"github.com/gol4ng/httpware/v4/metrics"
)

func Metrics(recorder metrics.Recorder, options ... metrics.Option) httpware.Middleware {
Expand Down
10 changes: 5 additions & 5 deletions middleware/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import (
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/stretchr/testify/assert"

"github.com/gol4ng/httpware/v3"
"github.com/gol4ng/httpware/v3/metrics"
prom "github.com/gol4ng/httpware/v3/metrics/prometheus"
"github.com/gol4ng/httpware/v3/middleware"
"github.com/gol4ng/httpware/v3/mocks"
"github.com/gol4ng/httpware/v4"
"github.com/gol4ng/httpware/v4/metrics"
prom "github.com/gol4ng/httpware/v4/metrics/prometheus"
"github.com/gol4ng/httpware/v4/middleware"
"github.com/gol4ng/httpware/v4/mocks"
)

func TestMetrics(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions middleware/rate_limit.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package middleware
import (
"net/http"

"github.com/gol4ng/httpware/v3"
"github.com/gol4ng/httpware/v3/rate_limit"
"github.com/gol4ng/httpware/v4"
"github.com/gol4ng/httpware/v4/rate_limit"
)

func RateLimit(limiter rate_limit.RateLimiter, options ...RateLimitOption) httpware.Middleware {
Expand Down
8 changes: 4 additions & 4 deletions middleware/rate_limit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (
"testing"
"time"

"github.com/gol4ng/httpware/v3"
"github.com/gol4ng/httpware/v3/middleware"
"github.com/gol4ng/httpware/v3/mocks"
"github.com/gol4ng/httpware/v3/rate_limit"
"github.com/gol4ng/httpware/v4"
"github.com/gol4ng/httpware/v4/middleware"
"github.com/gol4ng/httpware/v4/mocks"
"github.com/gol4ng/httpware/v4/rate_limit"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
Expand Down
18 changes: 18 additions & 0 deletions middleware/request_listener.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package middleware

import (
"net/http"

"github.com/gol4ng/httpware/v4"
)

func RequestListener(listeners ...func(*http.Request)) httpware.Middleware {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
for _, listener := range listeners {
listener(request)
}
next.ServeHTTP(writer, request)
})
}
}
36 changes: 36 additions & 0 deletions middleware/request_listener_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package middleware_test

import (
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"

"github.com/gol4ng/httpware/v4/middleware"
"github.com/stretchr/testify/assert"
)

func TestRequestListener(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "http://fake-addr", ioutil.NopCloser(strings.NewReader(url.Values{
"mykey": {"myvalue"},
}.Encode())))
responseWriter := &httptest.ResponseRecorder{}

handlerCalled := false
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, req, r)
handlerCalled = true
})

called := false
listenerMock := func(innerReq *http.Request) {
called = true
assert.Equal(t, req, innerReq)
}

middleware.RequestListener(listenerMock)(handler).ServeHTTP(responseWriter, req)
assert.True(t, called)
assert.True(t, handlerCalled)
}
4 changes: 2 additions & 2 deletions middleware/skip.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package middleware
import (
"net/http"

"github.com/gol4ng/httpware/v3"
"github.com/gol4ng/httpware/v3/skip"
"github.com/gol4ng/httpware/v4"
"github.com/gol4ng/httpware/v4/skip"
)

// Skip middleware is used to conditionnaly activate a middleware in function of request
Expand Down
4 changes: 2 additions & 2 deletions middleware/skip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"net/http/httptest"
"testing"

"github.com/gol4ng/httpware/v3"
"github.com/gol4ng/httpware/v3/middleware"
"github.com/gol4ng/httpware/v4"
"github.com/gol4ng/httpware/v4/middleware"
"github.com/stretchr/testify/assert"
)

Expand Down
Loading