Commit 8b718f1e authored by Terence Lee's avatar Terence Lee

remove header appending, and the last header defined always wins

parent 64dc2b1b
...@@ -132,6 +132,25 @@ For example, to enable CORS for all resources, you just need to enable it for al ...@@ -132,6 +132,25 @@ For example, to enable CORS for all resources, you just need to enable it for al
} }
``` ```
##### Precedence
When there are header conflicts, the last header definition always wins. The headers do not get appended. For example,
```json
{
"headers": {
"/**": {
"X-Foo": "bar",
"X-Bar": "baz"
},
"/foo": {
"X-Foo": "foo"
}
}
}
```
when accessing `/foo`, `X-Foo` will have the value `"foo"` and `X-Bar` will not be present.
### Route Ordering ### Route Ordering
* Root Files * Root Files
......
...@@ -9,12 +9,13 @@ req = Nginx::Request.new ...@@ -9,12 +9,13 @@ req = Nginx::Request.new
uri = req.var.uri uri = req.var.uri
if config["headers"] if config["headers"]
config["headers"].each do |route, header_hash| config["headers"].to_a.reverse.each do |route, header_hash|
if Regexp.compile("^#{NginxConfigUtil.to_regex(route)}$") =~ uri if Regexp.compile("^#{NginxConfigUtil.to_regex(route)}$") =~ uri
header_hash.each do |key, value| header_hash.each do |key, value|
# value must be a string # value must be a string
req.headers_out[key] = value.to_s req.headers_out[key] = value.to_s
end end
break
end end
end end
end end
{
"headers": {
"/": {
"Cache-Control": "no-cache"
},
"/**": {
"X-Foo": "false",
"X-Bar": "blah"
},
"/foo.html": {
"X-Foo": "true"
}
},
"clean_urls": true
}
...@@ -216,6 +216,18 @@ STATIC_JSON ...@@ -216,6 +216,18 @@ STATIC_JSON
end end
end end
describe "conflicting headers" do
let(:name) { "custom_headers_no_append" }
it "should not append headers" do
response = app.get("/foo.html")
expect(response["X-Foo"]).to eq("true")
expect(response["X-Bar"]).to eq(nil)
expect(response.code).to eq("200")
expect(response.body.chomp).to eq("foo")
end
end
describe "wildcard paths" do describe "wildcard paths" do
let(:name) { "custom_headers_wildcard" } let(:name) { "custom_headers_wildcard" }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment