HTTP Client

HTTP Request Example

This example shows the HTTP Client used to test the HTTP Server Books Example. full test.

1
2
3
private val client: Client by lazy {
    Client(AhcAdapter(), "http://localhost:${server.runtimePort}")
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
@Test fun `Create book returns 201 and new book ID`() {
    val result = client.post("/books?author=Vladimir%20Nabokov&title=Lolita")
    assert(Integer.valueOf(result.body) > 0)
    assert(201 == result.status)
}

@Test fun `List books contains all books IDs`() {
    val result = client.get("/books")
    assertResponseContains(result, "100", "101")
}

@Test fun `Get book returns all book's fields`() {
    val result = client.get("/books/101")
    assertResponseContains(result, "William Shakespeare", "Hamlet")
}

@Test fun `Update book overrides existing book data`() {
    val resultPut = client.put("/books/100?title=Don%20Quixote")
    assertResponseContains(resultPut, "100", "updated")

    val resultGet = client.get("/books/100")
    assertResponseContains(resultGet, "Miguel de Cervantes", "Don Quixote")
}

@Test fun `Delete book returns the deleted record ID`() {
    val result = client.delete("/books/102")
    assertResponseContains(result, "102", "deleted")
}

@Test fun `Book not found returns a 404`() {
    val result = client.get("/books/9999")
    assertResponseContains(result, 404, "not found")
}

@Test fun `Invalid method returns 405`() {
    val result = client.options("/books/9999")
    assert(405 == result.status)
}

Comments