Skip to content

Commit

Permalink
feat: file upload
Browse files Browse the repository at this point in the history
  • Loading branch information
artamonovkirill committed Mar 7, 2019
1 parent 441b697 commit 26d9b47
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 108 deletions.
20 changes: 16 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ limitations under the License.
* [Base url](#base-url)
* [Request headers](#request-headers)
* [Request body](#request-body)
* [File upload](#file-upload)
* [Handling responses](#responses)
* [Response status code](#status)
* [Response headers](#response-headers)
Expand Down Expand Up @@ -69,8 +70,10 @@ The library is initially intended for writing easily readable unit-tests but can
* (doc) maven usage and javadocs
* [1.2.0](http://mvnrepository.com/artifact/com.tomtom.http/goji-http-client/1.2.0)
* (feature) support for TRACE, OPTIONS and PATCH methods
* [1.2.3](https://search.maven.org/artifact/com.tomtom.http/goji-http-client/1.2.3/jar)
* (chore) updated dependencies, including jackson-databind version with vulnerabilities
* [1.2.3](https://search.maven.org/artifact/com.tomtom.http/goji-http-client/1.2.3/jar)
* (chore) updated dependencies, including jackson-databind version with vulnerabilities
* [1.3.0](https://search.maven.org/artifact/com.tomtom.http/goji-http-client/1.3.0/jar)
* (feat) file upload

<a id='usage'></a>
## Usage
Expand Down Expand Up @@ -109,8 +112,7 @@ http.options()
### A request to an arbitrary url:

```groovy
http.get(
url: 'http://pizza-delivery.org/margheritas')
http.get(url: 'http://pizza-delivery.org/margheritas')
```

<a id='base-url'></a>
Expand Down Expand Up @@ -152,6 +154,16 @@ http.put(
body: [key: 'value'])
```

<a id='file-upload'></a>
#### Uploading a file

If an instance of `java.io.File` is provided as `body` argument, it will be wrapped into a `MultipartFile`:
```groovy
http.put(
path: '/post',
body: '/tmp/input.json' as File)
```

<a id='responses'></a>
## Handling responses

Expand Down
21 changes: 13 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.tomtom.http</groupId>
<artifactId>goji-http-client</artifactId>
<version>1.2.4</version>
<version>1.3.0</version>
<name>GOJI HTTP Client</name>
<description>
A wrapper around Apache HttpClient and Jackson Databind libraries with lean Groovy syntax.
Expand Down Expand Up @@ -49,25 +49,25 @@
</scm>

<properties>
<groovy.version>2.5.5</groovy.version>
<httpclient.version>4.5.6</httpclient.version>
<groovy.version>2.5.6</groovy.version>
<httpclient.version>4.5.7</httpclient.version>
<jackson-databind.version>2.9.8</jackson-databind.version>

<gmavenplus-plugin.version>1.6.2</gmavenplus-plugin.version>
<maven-failsafe-plugin.version>3.0.0-M1</maven-failsafe-plugin.version>
<maven-failsafe-plugin.version>3.0.0-M3</maven-failsafe-plugin.version>
<maven-gpg-plugin.version>1.6</maven-gpg-plugin.version>
<groovydoc-maven-plugin.version>2.1</groovydoc-maven-plugin.version>
<maven-project-info-reports-plugin.version>3.0.0</maven-project-info-reports-plugin.version>
<maven-source-plugin.version>3.0.1</maven-source-plugin.version>
<maven-site-plugin.version>3.7.1</maven-site-plugin.version>
<maven-surefire-plugin.version>3.0.0-M1</maven-surefire-plugin.version>
<maven-surefire-plugin.version>3.0.0-M3</maven-surefire-plugin.version>
<nexus-staging-maven-plugin.version>1.6.8</nexus-staging-maven-plugin.version>

<cglib-nodep.version>3.2.10</cglib-nodep.version>
<log4j.version>2.11.1</log4j.version>
<slf4j.version>1.7.25</slf4j.version>
<log4j.version>2.11.2</log4j.version>
<slf4j.version>1.7.26</slf4j.version>
<spock.version>1.2-groovy-2.5</spock.version>
<wiremock.version>2.20.0</wiremock.version>
<wiremock.version>2.21.0</wiremock.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -112,6 +112,11 @@
<artifactId>httpclient</artifactId>
<version>${httpclient.version}</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>${httpclient.version}</version>
</dependency>

<dependency>
<groupId>org.codehaus.groovy</groupId>
Expand Down
119 changes: 56 additions & 63 deletions src/integration-test/groovy/com/tomtom/http/HttpClientIT.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,32 @@
package com.tomtom.http

import com.github.tomakehurst.wiremock.WireMockServer
import org.junit.Rule
import org.junit.rules.TemporaryFolder
import spock.lang.Specification

import static com.github.tomakehurst.wiremock.client.WireMock.*
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig
import static com.tomtom.http.response.ResponseCode.OK

class HttpClientIT extends Specification {

static service = new WireMockServer()

def service = new WireMockServer(wireMockConfig().dynamicPort())
def http = new HttpClient()

def setupSpec() {
service.start()
}
@Rule
TemporaryFolder tmp

def setup() {
service.resetAll()
service.start()
}

def 'Executes a get'() {
def 'executes a get'() {
given:
service.givenThat(
get(urlEqualTo('/path'))
.willReturn(
aResponse()
.withStatus(OK)
.withHeader('header', 'value')
.withBody('body')))
.willReturn(ok()
.withHeader('header', 'value')
.withBody('body')))

when:
def response = http.get(
Expand All @@ -58,33 +56,27 @@ class HttpClientIT extends Specification {
}
}

def 'Executes a get with path and base url'() {
def 'executes a get with path and base url'() {
given:
service.givenThat(
get(urlEqualTo('/path'))
.willReturn(
aResponse()
.withStatus(OK)))
.willReturn(ok()))
and:
def http = new HttpClient(
baseUrl: "http://localhost:${service.port()}")
def http = new HttpClient(baseUrl: "http://localhost:${service.port()}")

when:
def response = http.get(
path: '/path')
def response = http.get(path: '/path')

then:
response.statusCode == OK
}

def 'Executes a post with a body'() {
def 'executes a post with a body'() {
given:
service.givenThat(
post(urlEqualTo('/path'))
.withRequestBody(equalTo('body'))
.willReturn(
aResponse()
.withStatus(OK)))
.willReturn(ok()))

when:
def response = http.post(
Expand All @@ -95,14 +87,30 @@ class HttpClientIT extends Specification {
response.statusCode == OK
}

def 'Executes a put with a json body'() {
def 'executes a post with a file body'() {
given:
def file = tmp.newFile() << 'foo'
service.givenThat(
post(urlEqualTo('/path'))
.withMultipartRequestBody(
aMultipart().withBody(equalTo('foo')))
.willReturn(ok()))

when:
def response = http.post(
url: "http://localhost:${service.port()}/path",
body: file)

then:
response.statusCode == OK
}

def 'executes a put with a json body'() {
given:
service.givenThat(
put(urlEqualTo('/path'))
.withRequestBody(equalTo('{"a":"b"}'))
.willReturn(
aResponse()
.withStatus(OK)))
.willReturn(ok()))

when:
def response = http.put(
Expand All @@ -113,14 +121,12 @@ class HttpClientIT extends Specification {
response.statusCode == OK
}

def 'Executes a delete with headers'() {
def 'executes a delete with headers'() {
given:
service.givenThat(
delete(urlEqualTo('/path'))
.withHeader('header', equalTo('value'))
.willReturn(
aResponse()
.withStatus(OK)))
.willReturn(ok()))

when:
def response = http.delete(
Expand All @@ -131,58 +137,50 @@ class HttpClientIT extends Specification {
response.statusCode == OK
}

def 'Executes an options'() {
def 'executes options'() {
given:
service.givenThat(
options(urlEqualTo('/path'))
.willReturn(
aResponse().withStatus(OK)))
.willReturn(ok()))

when:
def response = http.options(
url: "http://localhost:${service.port()}/path")
def response = http.options(url: "http://localhost:${service.port()}/path")

then:
response.statusCode == OK
}

def 'Executes a trace'() {
def 'executes a trace'() {
given:
service.givenThat(
trace(urlEqualTo('/path'))
.willReturn(
aResponse().withStatus(OK)))
.willReturn(ok()))

when:
def response = http.trace(
url: "http://localhost:${service.port()}/path")
def response = http.trace(url: "http://localhost:${service.port()}/path")

then:
response.statusCode == OK
}

def 'Executes a patch'() {
def 'executes a patch'() {
given:
service.givenThat(
patch(urlEqualTo('/path'))
.willReturn(
aResponse().withStatus(OK)))
.willReturn(ok()))

when:
def response = http.patch(
url: "http://localhost:${service.port()}/path")
def response = http.patch(url: "http://localhost:${service.port()}/path")

then:
response.statusCode == OK
}

def 'Parses response'() {
def 'parses responses'() {
given:
service.givenThat(
get(urlEqualTo('/path'))
.willReturn(
aResponse()
.withStatus(OK)
.willReturn(ok()
.withBody('{"a": "b"}')))

when:
Expand All @@ -197,13 +195,11 @@ class HttpClientIT extends Specification {
}
}

def 'Parses complex response'() {
def 'parses generic responses'() {
given:
service.givenThat(
get(urlEqualTo('/path'))
.willReturn(
aResponse()
.withStatus(OK)
.willReturn(ok()
.withBody('[{"name": "John Doe"}]')))

when:
Expand All @@ -214,21 +210,18 @@ class HttpClientIT extends Specification {
then:
with(response) {
statusCode == OK
body == [
new Person(
name: 'John Doe')]
body == [new Person(name: 'John Doe')]
}
}

def cleanupSpec() {
def cleanup() {
service?.stop()
}


def 'Executes an https get'() {
def 'executes an https get'() {
when:
def response = http.get(
url: 'https://httpbin.org/html')
def response = http.get(url: 'https://httpbin.org/html')

then:
response.statusCode == OK
Expand Down
Loading

0 comments on commit 26d9b47

Please sign in to comment.