Skip to content

Commit

Permalink
Merge pull request #99 from ba-st/multiple_accept_lines
Browse files Browse the repository at this point in the history
Fix a bug when multiple Accept lines are coming in an incoming request.
  • Loading branch information
gcotelli authored Feb 5, 2020
2 parents 0cd5e2d + b598bcc commit 9ca5a9b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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 [

Expand Down
13 changes: 12 additions & 1 deletion source/Stargate-Model/RESTfulControllerAcceptNegotiator.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -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 [

Expand Down

0 comments on commit 9ca5a9b

Please sign in to comment.