-
Notifications
You must be signed in to change notification settings - Fork 653
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): Allow configurable headers for rest endpoints (#143)
* Allow configuration of arbitrary headers to rest endpoints * Allow customizable headers from a file * Allow customizable headers in the echo configuration yml
- Loading branch information
1 parent
03d9a6f
commit cae72ff
Showing
3 changed files
with
150 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
echo-rest/src/test/groovy/com/netflix/spinnaker/echo/config/RestConfigSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package com.netflix.spinnaker.echo.config | ||
|
||
import retrofit.RequestInterceptor | ||
import retrofit.RestAdapter | ||
import spock.lang.Specification | ||
import spock.lang.Subject | ||
|
||
class RestConfigSpec extends Specification { | ||
|
||
@Subject | ||
config = new RestConfig() | ||
|
||
def request = Mock(RequestInterceptor.RequestFacade) | ||
def EmptyHeadersFile = Mock(RestConfig.HeadersFromFile) | ||
def attacher = new RestConfig.RequestInterceptorAttacher() { | ||
RequestInterceptor interceptor | ||
@Override | ||
public void attach(RestAdapter.Builder builder, RequestInterceptor interceptor) { | ||
this.interceptor = interceptor | ||
} | ||
} | ||
|
||
void configureRestServices(RestProperties.RestEndpointConfiguration endpoint, RestConfig.HeadersFromFile headersFromFile) { | ||
RestProperties restProperties = new RestProperties(endpoints: [endpoint]) | ||
config.restServices(restProperties, config.retrofitClient(), config.retrofitLogLevel("BASIC"), attacher, headersFromFile) | ||
} | ||
|
||
void "Generate basic auth header"() { | ||
given: | ||
RestProperties.RestEndpointConfiguration endpoint = new RestProperties.RestEndpointConfiguration( | ||
url: "http://localhost:9090", | ||
username: "testuser", | ||
password: "testpassword") | ||
configureRestServices(endpoint, EmptyHeadersFile) | ||
|
||
when: | ||
attacher.interceptor.intercept(request) | ||
|
||
then: | ||
1 * request.addHeader("Authorization", "Basic dGVzdHVzZXI6dGVzdHBhc3N3b3Jk") | ||
0 * request.addHeader(_, _) | ||
} | ||
|
||
void "'Authorization' header over generated basic auth header"() { | ||
given: | ||
RestProperties.RestEndpointConfiguration endpoint = new RestProperties.RestEndpointConfiguration( | ||
url: "http://localhost:9090", | ||
username: "testuser", | ||
password: "testpassword", | ||
headers: ["Authorization": "FromConfig"]) | ||
configureRestServices(endpoint, EmptyHeadersFile) | ||
|
||
when: | ||
attacher.interceptor.intercept(request) | ||
|
||
then: | ||
1 * request.addHeader("Authorization", "FromConfig") | ||
0 * request.addHeader(_, _) | ||
} | ||
|
||
void "'Authorization' headerFile over all others"() { | ||
given: | ||
RestProperties.RestEndpointConfiguration endpoint = new RestProperties.RestEndpointConfiguration( | ||
url: "http://localhost:9090", | ||
username: "testuser", | ||
password: "testpassword", | ||
headers: ["Authorization": "FromConfig"], | ||
headersFile: "/testfile") | ||
RestConfig.HeadersFromFile headersFromFile = new RestConfig.HeadersFromFile() { | ||
@Override | ||
Map<String, String> headers(String path) { | ||
return [ | ||
"Authorization": "FromFile" | ||
] | ||
} | ||
} | ||
configureRestServices(endpoint, headersFromFile) | ||
|
||
when: | ||
attacher.interceptor.intercept(request) | ||
|
||
then: | ||
1 * request.addHeader("Authorization", "FromFile") | ||
0 * request.addHeader(_, _) | ||
} | ||
} |