Slide 1

Slide 1 text

Flute ‒ Go HTTP Client Testing Framework 2019-07-13 Shunsuke Suzuki 1

Slide 2

Slide 2 text

Who am I? https://github.com/suzuki-shunsuke/profile Backend and DevOps engineer in Japan I love Golang 2

Slide 3

Slide 3 text

Overview • Introduction of my OSS `flute`, which is the HTTP client testing framework for Golang • How to test HTTP request parameters and mock the API server 3

Slide 4

Slide 4 text

Do you write API clients of Go? 4

Slide 5

Slide 5 text

Do you write tests of API clients of Go? 5

Slide 6

Slide 6 text

Tests of the API client Request parameters • HTTP method, request path, query, header, request body, etc Response • parse of response body 6

Slide 7

Slide 7 text

Tests of the API client Request parameters • HTTP method, request path, query, header, request body, etc Response • parse of response body They are common to all API clients, so letʼs do them at the framework 7

Slide 8

Slide 8 text

Other libraries in awesome-go There are many libraries for testing of HTTP clients. • https://github.com/avelino/awesome-go#testing • https://github.com/h2non/gock • https://github.com/seborama/govcr • https://github.com/jarcoal/httpmock • https://github.com/tv42/mockhttp • https://github.com/gavv/httpexpect • https://github.com/dnaeon/go-vcr • https://github.com/h2non/baloo • etc 8

Slide 9

Slide 9 text

So why do I develop flute? • We can mock the HTTP server with them, but we canʼt test the request parameters with them • I used gock, but it is inconvenient that we canʼt understand why the request matches no mock 9

Slide 10

Slide 10 text

So why do I develop flute? • We can mock the HTTP server with them, but we canʼt test the request parameters with them • I used gock, but it is inconvenient that we canʼt understand why the request matches no mock I canʼt find the library meets my needs completely, so I decided to develop it by myself 10

Slide 11

Slide 11 text

How to write the mock server in Go • run the mock server with httptest • customize http.Clientʼs Transport 11

Slide 12

Slide 12 text

How to write the mock server in Go • run the mock server with httptest • customize http.Clientʼs Transport <- flute choices this 12

Slide 13

Slide 13 text

Flute ‒ HTTP client testing framework • https://github.com/suzuki-shunsuke/flute • HTTP client testing framework for Golang 13

Slide 14

Slide 14 text

Features • tests of request parameters • HTTP method, request path, query, header, request body, etc • mock the HTTP server 14

Slide 15

Slide 15 text

Example: API client to create a user 15

Slide 16

Slide 16 text

16 https://github.com/suzuki-shunsuke/flute/blob/master/examples/create_user.go

Slide 17

Slide 17 text

pass *flute.Transport to http.Client.Transport 17 https://github.com/suzuki-shunsuke/flute/blob/master/examples/create_user_test.go

Slide 18

Slide 18 text

18

Slide 19

Slide 19 text

19 1. match the request with Matcher 2. test the request with Tester 3. returns the response with Response

Slide 20

Slide 20 text

20 we can define the test case declaratively

Slide 21

Slide 21 text

assertion of the request parameters flute uses testify internally, so it is easy to understand why the test fails 21

Slide 22

Slide 22 text

22 ex. in case the request headers arenʼt expected

Slide 23

Slide 23 text

Conclusion • I develop the OSS `flute`, which is the HTTP client testing framework for Golang • With flute we can not only mock the API server but also test the request parameters • With flute we can define the tests declaratively • In Go, we can mock the HTTP server easily with http.RoundTripper 23