Skip to content

Commit

Permalink
Merge pull request #25 from ba-st/minor_improvements
Browse files Browse the repository at this point in the history
Introduce generic set header command
  • Loading branch information
gcotelli authored Nov 26, 2020
2 parents d79a953 + 6a168f5 commit 1ffd878
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 23 deletions.
22 changes: 22 additions & 0 deletions source/Superluminal-Model-Tests/HttpRequestTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,28 @@ HttpRequestTest >> testGetWithAuthorization [
]
]

{ #category : #tests }
HttpRequestTest >> testGetWithCustomHeader [

| httpRequest response |

httpRequest := HttpRequest
get: self httpbinLocation
configuredUsing: [ :request | request headers set: #Authorization to: 'Yes' ].

response := httpRequest applyOn: self newHttpClient.

self
assert: response isSuccess;
withJsonFromContentsIn: response
do: [ :json |
self
assert: json url equals: self httpbinLocation;
assert: json method equals: 'GET';
assert: json headers Authorization equals: 'Yes'
]
]

{ #category : #tests }
HttpRequestTest >> testGetWithEmptyConfiguration [

Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
Class {
#name : #SetAuthorizationHeaderCommandTest,
#name : #SetHeaderCommandTest,
#superclass : #TestCase,
#category : #'Superluminal-Model-Tests-Commands'
}

{ #category : #tests }
SetAuthorizationHeaderCommandTest >> testApplyOn [
SetHeaderCommandTest >> testApplyOn [

| httpClient command |

httpClient := ZnClient new.
command := SetAuthorizationHeaderCommand toBearerToken: 'token'.
command := SetHeaderCommand settingAuthorizationToBearerToken: 'token'.
command applyOn: httpClient.

self assert: ( httpClient request headers at: #Authorization ) equals: 'Bearer ''token'''
]

{ #category : #tests }
SetAuthorizationHeaderCommandTest >> testCannotUseAnEmptyValue [
SetHeaderCommandTest >> testCannotUseAnEmptyValue [

self
should: [ SetAuthorizationHeaderCommand to: '' ]
should: [ SetHeaderCommand settingAuthorizationTo: '' ]
raise: InstanceCreationFailed
withMessageText: 'An Authorization header cannot include empty directives.'
]
10 changes: 8 additions & 2 deletions source/Superluminal-Model/HttpRequestHeadersBuilder.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ Class {
#category : #'Superluminal-Model-Builders'
}

{ #category : #configuring }
HttpRequestHeadersBuilder >> set: aHeaderName to: aHeaderValue [

^ SetHeaderCommand setting: aHeaderName to: aHeaderValue
]

{ #category : #configuring }
HttpRequestHeadersBuilder >> setAcceptTo: aMediaType [

Expand All @@ -13,13 +19,13 @@ HttpRequestHeadersBuilder >> setAcceptTo: aMediaType [
{ #category : #configuring }
HttpRequestHeadersBuilder >> setAuthorizationTo: anAuthorizationDirective [

^ SetAuthorizationHeaderCommand to: anAuthorizationDirective
^ SetHeaderCommand settingAuthorizationTo: anAuthorizationDirective
]

{ #category : #configuring }
HttpRequestHeadersBuilder >> setBearerTokenTo: aBearerToken [

^ SetAuthorizationHeaderCommand toBearerToken: aBearerToken
^ SetHeaderCommand settingAuthorizationToBearerToken: aBearerToken
]

{ #category : #configuring }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,47 @@ Directives
"
Class {
#name : #SetAuthorizationHeaderCommand,
#name : #SetHeaderCommand,
#superclass : #HttpRequestCommand,
#instVars : [
'authorizationDirectives'
'headerName',
'headerValue'
],
#category : #'Superluminal-Model-Commands'
}

{ #category : #'instance creation' }
SetAuthorizationHeaderCommand class >> to: anAuthorizationDirective [
SetHeaderCommand class >> setting: aHeaderName to: aHeaderValue [

^ self new initializeSetting: aHeaderName to: aHeaderValue
]

{ #category : #'instance creation' }
SetHeaderCommand class >> settingAuthorizationTo: anAuthorizationDirective [

AssertionChecker
enforce: [ anAuthorizationDirective notEmpty ]
because: 'An Authorization header cannot include empty directives.'
raising: InstanceCreationFailed.

^ self new initializeTo: anAuthorizationDirective
^ self setting: #Authorization to: anAuthorizationDirective
]

{ #category : #'instance creation' }
SetAuthorizationHeaderCommand class >> toBearerToken: token [
SetHeaderCommand class >> settingAuthorizationToBearerToken: token [

^ self to: ( #'Bearer <1p>' expandMacrosWith: token )
^ self settingAuthorizationTo: ( #'Bearer <1p>' expandMacrosWith: token )
]

{ #category : #applying }
SetAuthorizationHeaderCommand >> applyOn: httpClient [
SetHeaderCommand >> applyOn: httpClient [

httpClient headerAt: #Authorization put: authorizationDirectives
httpClient headerAt: headerName put: headerValue
]

{ #category : #initialization }
SetAuthorizationHeaderCommand >> initializeTo: anAuthorizationDirective [
SetHeaderCommand >> initializeSetting: aHeaderName to: aHeaderValue [

authorizationDirectives := anAuthorizationDirective
headerName := aHeaderName.
headerValue := aHeaderValue
]
18 changes: 12 additions & 6 deletions source/Superluminal-RESTfulAPI/RESTfulAPIClient.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -212,17 +212,23 @@ RESTfulAPIClient >> patch: anEntity at: aLocation withSuccessfulResponseDo: aBlo
RESTfulAPIClient >> post: anEntity at: aLocation withSuccessfulResponseDo: aBlock [

^ self
handleExceptionsDuring: [ | httpRequest response |
postAt: aLocation
configuredBy: [ :request | request body contents: anEntity ]
withSuccessfulResponseDo: aBlock
]

httpRequest := HttpRequest
post: aLocation
configuredUsing: [ :request | request body contents: anEntity ].
{ #category : #invoking }
RESTfulAPIClient >> postAt: aLocation configuredBy: aRequestBuildingBlock withSuccessfulResponseDo: aBlock [

^ self
handleExceptionsDuring: [
| httpRequest response |
httpRequest := HttpRequest post: aLocation configuredUsing: aRequestBuildingBlock.
self
withHttpClientFor: aLocation
do: [ :httpClient | response := httpRequest applyOn: httpClient ].
response isSuccess
ifTrue: [
expiringCache clearResourceAt: aLocation.
ifTrue: [ expiringCache clearResourceAt: aLocation.
response isCreated then: [ self tryToCacheContentsOf: response basedOn: response location ].
aBlock value: response contents
]
Expand Down

0 comments on commit 1ffd878

Please sign in to comment.