diff --git a/source/Stargate-Model-Tests/RESTfulControllerAcceptNegotiatorTest.class.st b/source/Stargate-Model-Tests/RESTfulControllerAcceptNegotiatorTest.class.st index 52fe832..f7dbd86 100644 --- a/source/Stargate-Model-Tests/RESTfulControllerAcceptNegotiatorTest.class.st +++ b/source/Stargate-Model-Tests/RESTfulControllerAcceptNegotiatorTest.class.st @@ -57,6 +57,24 @@ RESTfulControllerAcceptNegotiatorTest >> testAPIExactMatch [ assertBestRepresentationFor: 'application/vnd.stargate.pet.summary+json;version=1.0.0' given: apiMediaTypes is: 'application/vnd.stargate.pet.summary+json;version=1.0.0' asMediaType ] +{ #category : #tests } +RESTfulControllerAcceptNegotiatorTest >> testAcceptHeaderContentIsAnArray [ + + "Fixes a bug discovered when using the Consul HTTP Check method and configuring an Accept header on the check" + + | request | + + request := ZnRequest + readFrom: + ( 'GET /example HTTP/1.1<1s><2s>User-Agent: Consul Agent<1s><2s>Accept: */*<1s><2s>Accept: application/json<1s><2s>' + expandMacrosWith: Character cr asString + with: Character lf asString ) readStream. + + self + assert: ( ( RESTfulControllerAcceptNegotiator basedOn: apiMediaTypes ) bestRepresentationFor: request ) + equals: 'application/vnd.stargate.pet+json;version=2.0.0' asMediaType +] + { #category : #tests } RESTfulControllerAcceptNegotiatorTest >> testAcceptingAnything [ diff --git a/source/Stargate-Model/RESTfulControllerAcceptNegotiator.class.st b/source/Stargate-Model/RESTfulControllerAcceptNegotiator.class.st index 90f4a32..0439d46 100644 --- a/source/Stargate-Model/RESTfulControllerAcceptNegotiator.class.st +++ b/source/Stargate-Model/RESTfulControllerAcceptNegotiator.class.st @@ -26,7 +26,12 @@ RESTfulControllerAcceptNegotiator >> acceptableMediaTypesIn: anHttpRequest [ on: KeyNotFound do: [ :signal | signal return: '*/*' ]. - ^ ( acceptHeaderContent splitOn: ',' ) collect: [ :type | type asMediaType ] + ^ acceptHeaderContent isArray + then: [ acceptHeaderContent + flatCollect: [ :acceptList | self splitMediaTypesIn: acceptList ] + as: OrderedCollection + ] + otherwise: [ self splitMediaTypesIn: acceptHeaderContent ] ] { #category : #querying } @@ -80,6 +85,12 @@ RESTfulControllerAcceptNegotiator >> sortByPrecedence: mediaTypes [ ^ mediaTypes sorted: [ :mediaType | self precedenceFor: mediaType ] descending ] +{ #category : #'private - accessing' } +RESTfulControllerAcceptNegotiator >> splitMediaTypesIn: acceptList [ + + ^ ( acceptList splitOn: ',' ) collect: [ :type | type asMediaType ] +] + { #category : #'private - accessing' } RESTfulControllerAcceptNegotiator >> versionIn: acceptable canBeHandledBy: available [