adding custom headers for remoteURL

This commit is contained in:
Julien Neuhart
2019-12-06 11:22:11 +01:00
parent 782f6ac27e
commit 927f98c66b
7 changed files with 71 additions and 39 deletions

View File

@@ -13,8 +13,8 @@ const (
WebhookURLCustomHeaderCanonicalBaseKey string = "Gotenberg-Webhookurl-"
)
func fetchCustomHeaders(r Resource, baseKey string) map[string][]string {
customHeaders := make(map[string][]string)
func fetchCustomHeaders(r Resource, baseKey string) map[string]string {
customHeaders := make(map[string]string)
for key, value := range r.customHeaders {
if strings.Contains(key, baseKey) {
realKey := strings.Replace(key, baseKey, "", 1)
@@ -26,12 +26,12 @@ func fetchCustomHeaders(r Resource, baseKey string) map[string][]string {
// RemoteURLCustomHeaders is a helper for retrieving
// the custom headers for the URL conversion.
func RemoteURLCustomHeaders(r Resource) map[string][]string {
func RemoteURLCustomHeaders(r Resource) map[string]string {
return fetchCustomHeaders(r, RemoteURLCustomHeaderCanonicalBaseKey)
}
// WebhookURLCustomHeaders is a helper for retrieving
// the custom headers for the webhook URL.
func WebhookURLCustomHeaders(r Resource) map[string][]string {
func WebhookURLCustomHeaders(r Resource) map[string]string {
return fetchCustomHeaders(r, WebhookURLCustomHeaderCanonicalBaseKey)
}

View File

@@ -18,17 +18,13 @@ func TestRemoteURLCustomHeaders(t *testing.T) {
customHeaderValue := "bar"
customHeaderCanonicalRealKey := "Foo"
customHeaderCanonicalKey := http.CanonicalHeaderKey(fmt.Sprintf("%s%s", RemoteURLCustomHeaderCanonicalBaseKey, customHeaderCanonicalRealKey))
r.WithCustomHeader(customHeaderCanonicalKey, []string{customHeaderValue})
r.WithCustomHeader("Bar", []string{"Bar"})
expected := map[string][]string{
customHeaderCanonicalRealKey: []string{
customHeaderValue,
},
r.WithCustomHeader(customHeaderCanonicalKey, customHeaderValue)
r.WithCustomHeader("Bar", "Bar")
expected := map[string]string{
customHeaderCanonicalRealKey: customHeaderValue,
}
notExpected := map[string][]string{
customHeaderCanonicalKey: []string{
customHeaderValue,
},
notExpected := map[string]string{
customHeaderCanonicalKey: customHeaderValue,
}
v := RemoteURLCustomHeaders(r)
assert.Equal(t, expected, v)
@@ -44,17 +40,13 @@ func TestWebhookURLCustomHeaders(t *testing.T) {
customHeaderValue := "bar"
customHeaderCanonicalRealKey := "Foo"
customHeaderCanonicalKey := http.CanonicalHeaderKey(fmt.Sprintf("%s%s", WebhookURLCustomHeaderCanonicalBaseKey, customHeaderCanonicalRealKey))
r.WithCustomHeader(customHeaderCanonicalKey, []string{customHeaderValue})
r.WithCustomHeader("Bar", []string{"Bar"})
expected := map[string][]string{
customHeaderCanonicalRealKey: []string{
customHeaderValue,
},
r.WithCustomHeader(customHeaderCanonicalKey, customHeaderValue)
r.WithCustomHeader("Bar", "Bar")
expected := map[string]string{
customHeaderCanonicalRealKey: customHeaderValue,
}
notExpected := map[string][]string{
customHeaderCanonicalKey: []string{
customHeaderValue,
},
notExpected := map[string]string{
customHeaderCanonicalKey: customHeaderValue,
}
v := WebhookURLCustomHeaders(r)
assert.Equal(t, expected, v)

View File

@@ -3,6 +3,7 @@ package resource
import (
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
@@ -25,7 +26,7 @@ const TemporaryDirectory string = "tmp"
type Resource struct {
logger xlog.Logger
dirPath string
customHeaders map[string][]string
customHeaders map[string]string
args map[ArgKey]string
files map[string]file
}
@@ -53,7 +54,7 @@ func New(logger xlog.Logger, directoryName string) (Resource, error) {
return Resource{
logger: logger,
dirPath: dirPath,
customHeaders: make(map[string][]string),
customHeaders: make(map[string]string),
args: make(map[ArgKey]string),
files: make(map[string]file),
}, nil
@@ -76,15 +77,17 @@ func (r Resource) Close() error {
// WithCustomHeader add a new custom header to the Resource.
// Given key should be in canonical format.
func (r *Resource) WithCustomHeader(key string, value []string) {
func (r *Resource) WithCustomHeader(key string, value string) {
const op string = "resource.Resource.WithCustomHeader"
if strings.Contains(key, RemoteURLCustomHeaderCanonicalBaseKey) ||
strings.Contains(key, WebhookURLCustomHeaderCanonicalBaseKey) {
r.customHeaders[key] = value
r.logger.DebugfOp(op, "added '%s' with value '%s' to resource custom headers", key, value)
// should already be in canonical format.
canonicalKey := http.CanonicalHeaderKey(key)
if strings.Contains(canonicalKey, RemoteURLCustomHeaderCanonicalBaseKey) ||
strings.Contains(canonicalKey, WebhookURLCustomHeaderCanonicalBaseKey) {
r.customHeaders[canonicalKey] = value
r.logger.DebugfOp(op, "added '%s' with value '%s' to resource custom headers", canonicalKey, value)
return
}
r.logger.DebugfOp(op, "skipping '%s' as it is not a custom header...", key)
r.logger.DebugfOp(op, "skipping '%s' as it is not a custom header...", canonicalKey)
}
// WithArg add a new argument to the Resource.