1
0
mirror of https://github.com/IBM/fp-go.git synced 2025-08-10 22:31:32 +02:00

fix: add WithBearer

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
This commit is contained in:
Dr. Carsten Leue
2023-12-19 14:33:52 +01:00
parent a6f55a199c
commit 3aa55c74d4
13 changed files with 125 additions and 17 deletions

View File

@@ -23,6 +23,8 @@ import (
ENDO "github.com/IBM/fp-go/endomorphism"
F "github.com/IBM/fp-go/function"
C "github.com/IBM/fp-go/http/content"
H "github.com/IBM/fp-go/http/headers"
IOE "github.com/IBM/fp-go/ioeither"
IOEH "github.com/IBM/fp-go/ioeither/http"
J "github.com/IBM/fp-go/json"
@@ -80,8 +82,10 @@ var (
O.Of[IOE.IOEither[error, []byte]],
Body.Set,
)
// WithContentType adds the content type header
WithContentType = WithHeader("Content-Type")
// WithContentType adds the [H.ContentType] header
WithContentType = WithHeader(H.ContentType)
// WithAuthorization adds the [H.Authorization] header
WithAuthorization = WithHeader(H.Authorization)
// WithGet adds the [http.MethodGet] method
WithGet = WithMethod(http.MethodGet)
@@ -92,6 +96,12 @@ var (
// WithDelete adds the [http.MethodDelete] method
WithDelete = WithMethod(http.MethodDelete)
// WithBearer creates a [BuilderBuilder] to add a Bearer [H.Authorization] header
WithBearer = F.Flow2(
S.Format[string]("Bearer %s"),
WithAuthorization,
)
// Requester creates a requester from a builder
Requester = (*Builder).Requester
@@ -187,7 +197,7 @@ func (builder *Builder) Requester() IOEH.Requester {
req.Header[name] = value
}
if rdr != nil {
req.Header.Set("Content-Length", strconv.FormatInt(rdr.Size(), 10))
req.Header.Set(H.ContentLength, strconv.FormatInt(rdr.Size(), 10))
}
}
return req, err
@@ -237,7 +247,7 @@ func WithFormData(value url.Values) Endomorphism {
IOE.Of[error, []byte],
WithBody,
),
WithContentType("application/x-www-form-urlencoded"),
WithContentType(C.FormEncoded),
)
}
@@ -250,6 +260,6 @@ func WithJson[T any](data T) Endomorphism {
IOE.FromEither[error, []byte],
WithBody,
),
WithContentType("application/json"),
WithContentType(C.Json),
)
}

View File

@@ -19,24 +19,26 @@ import (
"testing"
F "github.com/IBM/fp-go/function"
C "github.com/IBM/fp-go/http/content"
H "github.com/IBM/fp-go/http/headers"
O "github.com/IBM/fp-go/option"
"github.com/stretchr/testify/assert"
)
func TestBuiler(t *testing.T) {
name := "Content-type"
name := H.ContentType
withContentType := WithHeader(name)
withoutContentType := WithoutHeader(name)
b1 := F.Pipe1(
Default,
withContentType("application/json"),
withContentType(C.Json),
)
b2 := F.Pipe1(
b1,
withContentType("text/plain"),
withContentType(C.TextPlain),
)
b3 := F.Pipe1(
@@ -45,7 +47,7 @@ func TestBuiler(t *testing.T) {
)
assert.Equal(t, O.None[string](), Default.GetHeader(name))
assert.Equal(t, O.Of("application/json"), b1.GetHeader(name))
assert.Equal(t, O.Of("text/plain"), b2.GetHeader(name))
assert.Equal(t, O.Of(C.Json), b1.GetHeader(name))
assert.Equal(t, O.Of(C.TextPlain), b2.GetHeader(name))
assert.Equal(t, O.None[string](), b3.GetHeader(name))
}