Skip to content

Commit

Permalink
Merge pull request #23 from ba-st/22-CORS-not-working-on-error
Browse files Browse the repository at this point in the history
Fixed CORS failing after an error
  • Loading branch information
gcotelli authored Jan 25, 2019
2 parents 77e2b34 + 4985e4c commit c819660
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
I'm a test case for CrossOriginResourceSharingHandler
"
Class {
#name : #CrossOriginResourceSharingHandlerTest,
#name : #CrossOriginResourceSharingPreflightHandlerTest,
#superclass : #TestCase,
#category : #'Stargate-Model-Tests-CORS'
}

{ #category : #tests }
CrossOriginResourceSharingHandlerTest >> testEvaluationOnRequest [
CrossOriginResourceSharingPreflightHandlerTest >> testEvaluationOnRequest [

| handler response |

handler := CrossOriginResourceSharingHandler allowing: #('GET' 'POST').
handler := CrossOriginResourceSharingPreflightHandler allowing: #('GET' 'POST').
response := handler teaEvalActionOnRequest: (ZnRequest options: 'url').

self
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
Class {
#name : #CrossOriginResourceSharingAfterFilter,
#superclass : #Object,
#instVars : [
'allowedOrigins'
],
#category : #'Stargate-Model-CORS'
}

{ #category : #'instance creation' }
CrossOriginResourceSharingAfterFilter class >> allowing: origins [

^ self new initializeAllowing: origins
]

{ #category : #initialization }
CrossOriginResourceSharingAfterFilter >> initializeAllowing: origins [

allowedOrigins := origins
]

{ #category : #'tea action' }
CrossOriginResourceSharingAfterFilter >> teaEvalActionOnRequest: request response: response [

| requestOrigin requestOriginUrl |

requestOrigin := request headers at: 'Origin'.
requestOriginUrl := requestOrigin asUrl.

allowedOrigins
detect: [ :origin | origin = requestOriginUrl ]
ifFound: [ :allowedOrigin |
response headers
at: 'Access-Control-Allow-Origin' put: requestOrigin;
at: 'Vary' put: 'Origin' ].

^ response
]
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Class {
#name : #CrossOriginResourceSharingHandler,
#name : #CrossOriginResourceSharingPreflightHandler,
#superclass : #Object,
#instVars : [
'httpMethods'
Expand All @@ -8,25 +8,25 @@ Class {
}

{ #category : #'instance creation' }
CrossOriginResourceSharingHandler class >> allowing: anHttpMethodsCollection [
CrossOriginResourceSharingPreflightHandler class >> allowing: anHttpMethodsCollection [

^ self new initializeAllowing: anHttpMethodsCollection
]

{ #category : #'private - accessing' }
CrossOriginResourceSharingHandler >> commaSeparatedHttpMethods [
CrossOriginResourceSharingPreflightHandler >> commaSeparatedHttpMethods [

^ (CollectionFormatter separatingWith: ', ') format: httpMethods
]

{ #category : #initialization }
CrossOriginResourceSharingHandler >> initializeAllowing: anHttpMethodsCollection [
CrossOriginResourceSharingPreflightHandler >> initializeAllowing: anHttpMethodsCollection [

httpMethods := anHttpMethodsCollection
]

{ #category : #evaluating }
CrossOriginResourceSharingHandler >> teaEvalActionOnRequest: aRequest [
CrossOriginResourceSharingPreflightHandler >> teaEvalActionOnRequest: aRequest [

| response |

Expand Down
24 changes: 8 additions & 16 deletions source/Stargate-Model/HTTPBasedRESTfulAPI.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,7 @@ HTTPBasedRESTfulAPI class >> configuredBy: configuration installing: aRESTfulCon
HTTPBasedRESTfulAPI >> beCORSAwareAllowing: origins [

teapotServer
after:
'/*'
-> [ :request :response |
| requestOrigin requestOriginUrl |

requestOrigin := request headers at: 'Origin'.
requestOriginUrl := requestOrigin asUrl.
origins
detect: [ :origin | origin = requestOriginUrl ]
ifFound: [ :allowedOrigin |
response headers
at: 'Access-Control-Allow-Origin' put: requestOrigin;
at: 'Vary' put: 'Origin' ].
response ];
after: '/*' -> (CrossOriginResourceSharingAfterFilter allowing: origins);
when: [ :request | request headers includesKey: 'Origin' ]
]

Expand All @@ -58,16 +45,21 @@ HTTPBasedRESTfulAPI >> configureRoutes [
{ #category : #initialization }
HTTPBasedRESTfulAPI >> initializeConfiguredBy: configuration installing: aRESTfulControllerCollection [

teapotServer := Teapot
configure: configuration , {(#notFoundHandlerClass -> Tea405AwareNotFoundHandler)}.
teapotServer := Teapot configure: configuration , {(#notFoundHandlerClass -> Tea405AwareNotFoundHandler)}.
controllers := aRESTfulControllerCollection.
errorHandlers := OrderedCollection new.

self
on: HTTPClientError
addErrorHandler: [ :clientError :request |
| json |

json := NeoJSONWriter toStringPretty: clientError.
(ZnResponse statusCode: clientError code)
headers:
(ZnHeaders defaultResponseHeaders
at: 'Access-Control-Allow-Origin' put: '*';
yourself);
entity: (ZnEntity json: json);
yourself ]
]
Expand Down
2 changes: 1 addition & 1 deletion source/Stargate-Model/RouteConfigurator.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ RouteConfigurator >> configureCrossOriginSharingRoutes [
teapot
OPTIONS:
resourceLocation
-> (CrossOriginResourceSharingHandler allowing: (routesAllowingCors at: resourceLocation)) ]
-> (CrossOriginResourceSharingPreflightHandler allowing: (routesAllowingCors at: resourceLocation)) ]
]

{ #category : #'private - configuring' }
Expand Down

0 comments on commit c819660

Please sign in to comment.