Skip to content

Commit

Permalink
Update testing to match repo
Browse files Browse the repository at this point in the history
  • Loading branch information
jtszalay committed Oct 18, 2023
1 parent a26898e commit d39355a
Show file tree
Hide file tree
Showing 7 changed files with 417 additions and 167 deletions.
22 changes: 11 additions & 11 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ type HTTP struct {

WebhookVerification *webhookVerification `json:"webhook_verification,omitempty"`

RequestHeader *httpHeaders `json:"request_header,omitempty"`
RequestHeader *httpRequestHeaders `json:"request_header,omitempty"`

ResponseHeader *httpHeaders `json:"header,omitempty"`
ResponseHeader *httpResponseHeaders `json:"header,omitempty"`

l *zap.Logger
}
Expand Down Expand Up @@ -159,23 +159,23 @@ func (t *HTTP) provisionOpts(ctx caddy.Context) error {
if err != nil {
return fmt.Errorf("provisioning webhook_verification: %v", err)
}
t.opts = append(t.opts, t.WebhookVerification.WebhookVerificationOption)
t.opts = append(t.opts, t.WebhookVerification.opt)
}

if t.RequestHeader != nil {
requestHeaderOpts, err := t.RequestHeader.provisionRequestHeaders()
err := t.RequestHeader.Provision(ctx)
if err != nil {
return fmt.Errorf("provisioning request_header: %v", err)
}
t.opts = append(t.opts, requestHeaderOpts...)
t.opts = append(t.opts, t.RequestHeader.Opts...)
}

if t.ResponseHeader != nil {
responseHeaderOpts, err := t.ResponseHeader.provisionResponseHeaders()
err := t.ResponseHeader.Provision(ctx)
if err != nil {
return fmt.Errorf("provisioning header: %v", err)
}
t.opts = append(t.opts, responseHeaderOpts...)
t.opts = append(t.opts, t.ResponseHeader.Opts...)
}

return nil
Expand Down Expand Up @@ -458,8 +458,8 @@ func (t *HTTP) unmarshalWebhookVerification(d *caddyfile.Dispenser) error {
}

func (t *HTTP) unmarshalRequestHeader(d *caddyfile.Dispenser) error {
requestHeader := httpHeaders{}
err := requestHeader.unmarshalHeaders(d)
requestHeader := httpRequestHeaders{}
err := requestHeader.UnmarshalCaddyfile(d)
if err != nil {
return d.Errf(`parsing request_header %w`, err)
}
Expand All @@ -470,8 +470,8 @@ func (t *HTTP) unmarshalRequestHeader(d *caddyfile.Dispenser) error {
}

func (t *HTTP) unmarshalResponseHeader(d *caddyfile.Dispenser) error {
responseHeader := httpHeaders{}
err := responseHeader.unmarshalHeaders(d)
responseHeader := httpResponseHeaders{}
err := responseHeader.UnmarshalCaddyfile(d)
if err != nil {
return d.Errf(`parsing header %w`, err)
}
Expand Down
44 changes: 10 additions & 34 deletions http_headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,14 @@ import (
)

type httpHeaders struct {
opts []config.HTTPEndpointOption
Opts []config.HTTPEndpointOption

Added map[string]string `json:"added,omitempty"`
Removed []string `json:"removed,omitempty"`
}

func (h *httpHeaders) provisionRequestHeaders() ([]config.HTTPEndpointOption, error) {

for name, value := range h.Added {
h.opts = append(h.opts, config.WithRequestHeader(name, value))
}

for _, name := range h.Removed {
h.opts = append(h.opts, config.WithRemoveRequestHeader(name))
}

return h.opts, nil
}

func (h *httpHeaders) provisionResponseHeaders() ([]config.HTTPEndpointOption, error) {

for name, value := range h.Added {
h.opts = append(h.opts, config.WithResponseHeader(name, value))
}

for _, name := range h.Removed {
h.opts = append(h.opts, config.WithRemoveResponseHeader(name))
}

return h.opts, nil
}

func (h *httpHeaders) doReplace(repl *caddy.Replacer) error {
func (h *httpHeaders) doReplace() {
repl := caddy.NewReplacer()

replacedAddedHeaders := make(map[string]string, len(h.Added))

Expand All @@ -66,20 +41,20 @@ func (h *httpHeaders) doReplace(repl *caddy.Replacer) error {

h.Removed = replacedRemovedHeaders

return nil
}

func (h *httpHeaders) unmarshalHeaders(d *caddyfile.Dispenser) error {
func (h *httpHeaders) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
for d.Next() {
// first see if headers are in the initial line
var hasArgs bool
if d.NextArg() {
hasArgs = true
field := d.Val()
var value string
if !d.AllArgs(&value) {
d.ArgErr() // Additional arg Would be replacement if ngrok handled that
if d.CountRemainingArgs() > 1 {
return d.ArgErr() // Additional arg Would be replacement if ngrok handled that
}
d.Args(&value)
err := h.applyHeaderOp(
field,
value,
Expand All @@ -102,9 +77,10 @@ func (h *httpHeaders) unmarshalHeaders(d *caddyfile.Dispenser) error {
field = strings.TrimSuffix(field, ":")

var value string
if !d.AllArgs(&value) {
d.ArgErr() // Additional arg Would be replacement if ngrok handled that
if d.CountRemainingArgs() > 1 {
return d.ArgErr() // Additional arg Would be replacement if ngrok handled that
}
d.Args(&value)
err := h.applyHeaderOp(
field,
value,
Expand Down
122 changes: 0 additions & 122 deletions http_headers_test.go

This file was deleted.

24 changes: 24 additions & 0 deletions http_request_headers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package ngroklistener

import (
"github.com/caddyserver/caddy/v2"
"golang.ngrok.com/ngrok/config"
)

type httpRequestHeaders struct {
httpHeaders
}

func (h *httpRequestHeaders) Provision(caddy.Context) error {
h.doReplace()

for name, value := range h.Added {
h.Opts = append(h.Opts, config.WithRequestHeader(name, value))
}

for _, name := range h.Removed {
h.Opts = append(h.Opts, config.WithRemoveRequestHeader(name))
}

return nil
}
Loading

0 comments on commit d39355a

Please sign in to comment.