Skip to content

Commit

Permalink
Merge pull request #64 from ba-st/support_QUERY
Browse files Browse the repository at this point in the history
Add support for HTTP `QUERY` method
  • Loading branch information
gcotelli authored Jun 9, 2023
2 parents b2d5a26 + 274d4f2 commit 8f94739
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 11 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Contributing

There's several ways to contribute to the project: reporting bugs, sending
There are several ways to contribute to the project: reporting bugs, sending
feedback, proposing ideas for new features, fixing or adding documentation,
promoting the project, or even contributing code.

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Building blocks for Internet technologies on top of [Zinc HTTP Components](https

## Installation

To load the project in a Pharo image follow this [instructions](docs/how-to/how-to-load-in-pharo.md).
To load the project in a Pharo image follow these [instructions](docs/how-to/how-to-load-in-pharo.md).

## Contributing

Expand Down
6 changes: 3 additions & 3 deletions docs/reference/Errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
This is an exception expecting to be raised when someone makes an incorrect HTTP
request.

It allows to handle any kind of HTTP client errors by doing something like:
It allows handling any kind of HTTP client errors by doing something like:

```smalltalk
[ ... ] on: HTTPClientError do: [:signal | ]
Expand Down Expand Up @@ -35,7 +35,7 @@ I represent an HTTP client error: `406 Not Acceptable`

The resource identified by the request is only capable of generating response
entities that have content characteristics not acceptable according to the
accept headers sent in the request.
`Accept` headers sent in the request.

I will carry over information about the acceptable media types.

Expand All @@ -50,7 +50,7 @@ accept headers sent in the request.
This is an exception expecting to be raised when the server encounters an
unexpected error.

It allows to handle any kind of HTTP server errors by doing something like:
It allows handling any kind of HTTP server errors by doing something like:

```smalltalk
[ ... ] on: HTTPServerError do: [:signal | ]
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/IETF.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ LanguageRange fromString: 'es-AR'.
For example:

```smalltalk
(LanguangeRage fromString: 'es') matches: 'es-AR' asLanguageTag "==> true"
(LanguageRange fromString: 'es') matches: 'es-AR' asLanguageTag "==> true"
```

**References:**
Expand Down
6 changes: 3 additions & 3 deletions docs/reference/Resilience.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Retry
configuredBy: [:retry | "configuration options" ]
```

The block to be evaluated can optionally receive the current attempt number in
The block to be evaluated can optionally receive the current attempt number in
a block argument:

```smalltalk
Expand All @@ -32,8 +32,8 @@ Retry
These options are sent to the `retry` instance provided in the second block.

- `upTo: retryCount` : The maximum number of retries. Defaults to 2.
- `every: duration` : Wait for a time duration between retry attempts. By
default don't wait.
- `every: duration` : Wait for a time duration between retry attempts. By default,
don't wait.
- `backoffExponentiallyWithTimeSlot: duration` : Wait for a time duration
between retry attempts determined by using the [exponential backoff algorithm](https://en.wikipedia.org/wiki/Exponential_backoff)
with a `duration` time slot.
Expand Down
6 changes: 4 additions & 2 deletions docs/reference/Zinc.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@

## ZnUrl

- `queryAt:putUrl:` allows to set a query parameter containing an URL that
- `queryAt:putUrl:` allows setting a query parameter containing a URL that
will be URL-encoded
- `start:limit:` allows to set two query parameters (`start` and `limit`)
- `start:limit:` allows setting two query parameters (`start` and `limit`)
usually used in pagination schemes.
- `asHostedAt:` provides a copy of the URL using as host, scheme, and port
the ones in the parameter.
Expand All @@ -35,13 +35,15 @@
- `setAccept:` configures the `Accept` header in the current request
- `setIfMatchTo:` configures the `If-Match` header in the current request
- `setIfNoneMatchTo:` configures the `If-None-Match` header in the current request
- `query` adds support for HTTP [QUERY method](https://www.ietf.org/archive/id/draft-ietf-httpbis-safe-method-w-body-02.html)

## ZnRequest

- `acceptLanguage` provides access to the `Accept-Language` header
- `setAcceptLanguage:` sets the `Accept-Language` header
- `setIfMatchTo:` configures the `If-Match` header
- `setIfNoneMatchTo:` configures the `If-None-Match` header
- `query:` creates an HTTP [QUERY request](https://www.ietf.org/archive/id/draft-ietf-httpbis-safe-method-w-body-02.html)

## ZnResponse

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,36 @@ Class {
#category : #'Hyperspace-Model-Tests-Zinc'
}

{ #category : #private }
ZnClientHyperspaceExtensionsTest >> handleRequest: request [

self
assert: request method equals: #QUERY;
assertUrl: request url equals: 'hello'.
^ ZnResponse ok: (ZnEntity text: 'hello')
]

{ #category : #tests }
ZnClientHyperspaceExtensionsTest >> testQuery [

| server port |

port := self freeListeningTCPPort.
server := ZnServer on: port.
server delegate: self.
server start.

[
| client |

client := ZnClient new.
client url: ('http://localhost:<1p>/hello' expandMacrosWith: port).
self
assert: client query equals: 'hello';
assert: client response isSuccess
] ensure: [ server stop ]
]

{ #category : #tests }
ZnClientHyperspaceExtensionsTest >> testResetRequest [

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,35 @@ ZnRequestHyperspaceExtensionsTest >> testIfNoneMatch [
self assert: (request headers at: #'If-None-Match') equals: '"12345"'
]

{ #category : #tests }
ZnRequestHyperspaceExtensionsTest >> testQuery [

| request output |

request := ZnRequest query: self googleUrl / 'search'.

self assert: request method equals: #QUERY.
output := String streamContents: [ :s | request writeOn: s ].

self
assert: (output includesSubstring: 'QUERY /search HTTP/1.1');
assert: (output includesSubstring: 'Host: google.com')
]

{ #category : #tests }
ZnRequestHyperspaceExtensionsTest >> testReadQueryRequest [

| request |

request := ZnRequest readFrom:
'QUERY /search HTTP/1.1<r><l>Host: google.com' expandMacros readStream.

self
assert: request method equals: #QUERY;
assertUrl: request url equals: 'search';
assertUrl: request host equals: 'http://google.com'
]

{ #category : #tests }
ZnRequestHyperspaceExtensionsTest >> testSetAcceptLanguageWithAny [

Expand Down
11 changes: 11 additions & 0 deletions source/Hyperspace-Model/ZnClient.extension.st
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ ZnClient >> logLevel [
^ logLevel
]

{ #category : #'*Hyperspace-Model' }
ZnClient >> query [
"Execute an HTTP QUERY the request set up and return the response #contents."

^ ZnRequest supportQUERYDuring: [
self
method: #QUERY;
execute
]
]

{ #category : #'*Hyperspace-Model' }
ZnClient >> resetRequest [

Expand Down
20 changes: 20 additions & 0 deletions source/Hyperspace-Model/ZnRequest.extension.st
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ ZnRequest >> hasLanguageProrityList [
^ self headers includesKey: 'Accept-Language'
]

{ #category : #'*Hyperspace-Model' }
ZnRequest class >> query: urlObject [

^ self supportQUERYDuring: [ self method: #QUERY url: urlObject ]
]

{ #category : #'*Hyperspace-Model' }
ZnRequest >> readFrom: stream [

^ self class supportQUERYDuring: [ super readFrom: stream ]
]

{ #category : #'*Hyperspace-Model' }
ZnRequest >> setAcceptLanguage: acceptLanguageDirectives [

Expand All @@ -29,3 +41,11 @@ ZnRequest >> setIfNoneMatchTo: etag [

self headers at: 'If-None-Match' put: etag asString
]

{ #category : #'*Hyperspace-Model' }
ZnRequest class >> supportQUERYDuring: aBlock [

^ aBlock
on: ZnUnknownHttpMethod
do: [ :signal | signal resume: signal method ]
]

0 comments on commit 8f94739

Please sign in to comment.