Skip to content

Commit

Permalink
Add support for setting headers on the proxy (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
steinfletcher authored Nov 25, 2019
1 parent 31291f5 commit 9f6d40d
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 7 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ See `examples/config.json`
"path_pattern": "/test-ui/(.*)",
"to": "/$1",
}],
"proxy_pass_headers": { // additional proxy headers. Optional
"Referer": "https://www.test.example.com"
}
}
```

Expand Down
13 changes: 7 additions & 6 deletions domain/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ type Config struct {
}

type Route struct {
Type string `json:"type"`
PathPattern *PathPattern `json:"path_pattern"`
Backend *Backend `json:"backend"`
Mock *Mock `json:"mock"`
Rewrite []Rewrite `json:"rewrite"`
Redirect *Redirect `json:"redirect"`
Type string `json:"type"`
PathPattern *PathPattern `json:"path_pattern"`
Backend *Backend `json:"backend"`
Mock *Mock `json:"mock"`
Rewrite []Rewrite `json:"rewrite"`
Redirect *Redirect `json:"redirect"`
ProxyPassHeaders map[string]string `json:"proxy_pass_headers"`
}

type Rewrite struct {
Expand Down
5 changes: 4 additions & 1 deletion examples/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@
"rewrite": [{
"path_pattern": "/test-ui/(.*)",
"to": "/$1"
}]
}],
"proxy_pass_headers": {
"Referer": "https://www.test1.example.co.uk/"
}
},
{
"type": "redirect",
Expand Down
5 changes: 5 additions & 0 deletions proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ func director(defaultBackend *url.URL, logger *log.Logger) func(req *http.Reques
break
}
}

// set any proxy pass headers from config
for name, value := range route.ProxyPassHeaders {
req.Header.Set(name, value)
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions proxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ func otherProxyMock(status int, responseBody string) *apitest.Mock {

func userProxyMock(status int, responseBody string) *apitest.Mock {
return apitest.NewMock().Get("http://localhost:3001/test-ui/users/info").
Header("Referer", "https://www.test.example.com").
RespondWith().
Status(status).
Body(responseBody).
Expand Down Expand Up @@ -267,6 +268,9 @@ func config() domain.Config {
Type: "proxy",
PathPattern: &domain.PathPattern{Regexp: regexp.MustCompile("^/test-ui/users/.*")},
Backend: &domain.Backend{URL: mockProxyUrlUserUi},
ProxyPassHeaders: map[string]string{
"Referer": "https://www.test.example.com",
},
},
{
Type: "proxy",
Expand Down

0 comments on commit 9f6d40d

Please sign in to comment.