Skip to content

Add support for returning custom response headers from a list service #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@ plugin.xml
*en_PS.properties
test/test-restful-api/web-app/css/**-rtl.css
/plugin.xml
/.settings
/target-eclipse
/.classpath
/.project
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,11 @@ class RestfulApiController {
holder.addHeader(totalCountHeader, count)
holder.addHeader(pageOffsetHeader, requestParams.offset ? requestParams?.offset : 0)
holder.addHeader(pageMaxHeader, requestParams.max ? requestParams?.max : result.size())
if (result.hasProperty("httpResponseHeaders") && result.httpResponseHeaders instanceof Map) {
result.httpResponseHeaders.each {
if (it.value != null) holder.addHeader(it.key, it.value.toString())
}
}
renderSuccessResponse( holder, 'default.rest.list.message' )
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,19 @@ package net.hedtech.restfulapi
class PagedResultArrayList extends ArrayList implements PagedResultList {

private long totalCount
private Map httpResponseHeaders

PagedResultArrayList(Collection c, long totalCount) {
PagedResultArrayList(Collection c, long totalCount, Map httpResponseHeaders = null) {
super(c)
this.totalCount = totalCount
this.httpResponseHeaders = httpResponseHeaders
}

long getTotalCount() {
totalCount
}

Map getHttpResponseHeaders() {
httpResponseHeaders
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2108,6 +2108,53 @@ class RestfulApiControllerSpec extends Specification {
200 == response.status
'2' == response.getHeaderValue( 'X-hedtech-totalCount' )
}

def "Test HTTP Response Headers"() {
setup:
//use default extractor for any methods with a request body
config.restfulApiConfig = {
resource 'things' config {
representation {
mediaTypes = ['application/json']
extractor = new DefaultJSONExtractor()
}
}
}
controller.init()

//mock the appropriate service method, expect exactly 1 invocation
def mock = Mock(ThingService)
mock.list(_) >> {return new PagedResultArrayList([[name:'foo']], 5,
["X-hedtech-responseHeader1":"value-1",
"X-hedtech-responseHeader2":42, // used to verify number converted to string
"X-hedtech-responseHeader3":null, // used to verify null valie is omitted
"X-hedtech-responseHeader4":"value-4"])}
controller.metaClass.getService = {-> mock}

mockCacheHeaders()

request.addHeader( 'Accept', 'application/json' )
//incoming format always json, so no errors
request.addHeader( 'Content-Type', 'application/json' )
params.pluralizedResourceName = 'things'

when:
controller.list()
def json = JSON.parse response.text

then:
0*mock.count(_) >> {}
200 == response.status
'5' == response.getHeaderValue( 'X-hedtech-totalCount' )
1 == json.size()
'foo' == json[0].name

// verify response headers
'value-1' == response.getHeaderValue( 'X-hedtech-responseHeader1' )
'42' == response.getHeaderValue( 'X-hedtech-responseHeader2' )
null == response.getHeaderValue( 'X-hedtech-responseHeader3' )
'value-4' == response.getHeaderValue( 'X-hedtech-responseHeader4' )
}


private void mockCacheHeaders() {
Expand Down