From 4da23929c21ad6a7c558f6edcf6df2d21a454fa6 Mon Sep 17 00:00:00 2001 From: clavinjune <24659468+clavinjune@users.noreply.github.com> Date: Sun, 31 Dec 2023 20:11:06 +0700 Subject: [PATCH] build Sun, 31 Dec 2023 20:11:06 +0700 Signed-off-by: clavinjune <24659468+clavinjune@users.noreply.github.com> --- config.toml | 8 +- .../en/blogs/golang-testing-http-handler.md | 99 +++++++++++++++++++ netlify.toml | 4 +- themes/june | 2 +- 4 files changed, 106 insertions(+), 7 deletions(-) create mode 100644 content/en/blogs/golang-testing-http-handler.md diff --git a/config.toml b/config.toml index 362b3784..10c43612 100644 --- a/config.toml +++ b/config.toml @@ -133,11 +133,11 @@ theme = 'june' # url = '/works/' # weight = 4 -[author] - Name = "Clavin June" - Email = 'juneardoc@gmail.com' - [params] + [params.author] + name = "Clavin June" + email = 'juneardoc@gmail.com' + [params.june] braveRewardsVerification = 'ze0dec1b3acfbd430cac887ce390fdefcb56679334fcd39bc49d434660a52e26c' # canonicalURL = 'https://another-page.com' diff --git a/content/en/blogs/golang-testing-http-handler.md b/content/en/blogs/golang-testing-http-handler.md new file mode 100644 index 00000000..1c73a1b6 --- /dev/null +++ b/content/en/blogs/golang-testing-http-handler.md @@ -0,0 +1,99 @@ +--- +title: "Golang Testing HTTP Handler" +date: 2023-12-31T19:52:21+07:00 +draft: false +iscjklanguage: false +isarchived: false +categories: ["tech"] +images: ["https://images.unsplash.com/photo-1508921340878-ba53e1f016ec?w=1920&q=50"] +aliases: [] +description: "testing HTTP handler using httptest standard library" +--- + +{{< unsplash user="@bugsster" src="photo-1508921340878-ba53e1f016ec" q="50" >}} + +## FaFiFu + +> This section is a bullshittery, you can skip it to the [main section](#introduction) + +It's been a year since my last blog post, 2023 is quite a year that makes me hard to spend time updating this blog. The thing is, going back working from the office take hours of productive hour. Going to the office preparation, commuting, chit-chat, snack time makes me less productive. Nah, that's maybe the me issue, just trying to blame others because it's easier :D. Just hoping that I will have enough time to update this blog more frequent in 2024. Enough of the chit-chat, let's go to the main section. + +## Introduction + +When you start working with Golang as a web server and developing some HTTP endpoints, you might want to test it as well. In Golang, in order to test the HTTP endpoints, it has a specific library called [httptest](https://pkg.go.dev/net/http/httptest). I also have created one blog post that highlight one specific use case of httptest related to mocking external HTTP call, you can read it [here](/blogs/mocking-http-call-in-golang-a-better-way/). + +## Components + +To create a unit test for testing HTTP endpoints, we can utilize [httptest.NewRecorder](https://pkg.go.dev/net/http/httptest#NewRecorder) and [httptest.NewRequest](https://pkg.go.dev/net/http/httptest#NewRequest). If you remember, golang basic [http.Handler](https://pkg.go.dev/net/http#Handler) looks like below: + +```go +func FooHandler(w http.ResponseWriter, r *http.Request) {} +``` + +It needs both [http.ResponseWriter](https://pkg.go.dev/net/http#ResponseWriter) and [http.Request](https://pkg.go.dev/net/http#Request), which the value of both of them will be provided by [http.Server](https://pkg.go.dev/net/http#Server) whenever a new request is coming to the handler. In testing, instead of running an `http.Server` that will provide the value, you can fill the argument using `httptest.NewRecorder` and `httptest.NewRequest` functions return value since both of the function consecutively return `http.ResponseWriter` and `http.Request`. + + +## Code + +Now let's go to the code. Let's say you have this handler that you want to test: + +```go +package handler + +import ( + "fmt" + "net/http" +) + +func RootHandler(w http.ResponseWriter, r *http.Request) { + fmt.Fprintf(w, "[%s] %s OK!", r.Method, r.URL.String()) +} +``` + +It's quite a simple handler, let's create a test for `RootHandler`: + +```go +package handler_test + +import ( + "fmt" + "io" + "net/http" + "net/http/httptest" + "testing" + + "github.com/clavinjune/example/handler" +) + +func TestRootHandler(t *testing.T) { + method := http.MethodPost + endpoint := "/foo" + + w := httptest.NewRecorder() + r := httptest.NewRequest(method, endpoint, http.NoBody) + defer r.Body.Close() + + handler.RootHandler(w, r) + defer w.Result().Body.Close() + + if w.Result().StatusCode != http.StatusOK { + t.Fatalf("want: %03d, got: %03d", http.StatusOK, w.Result().StatusCode) + } + + b, err := io.ReadAll(w.Result().Body) + if err != nil { + t.Fatalf("want: nil, got: %+q", err) + } + + want := fmt.Sprintf(`[%s] %s OK!`, method, endpoint) + got := string(b) + + if want != got { + t.Fatalf("want: %+q, got: %+q", want, got) + } +} +``` + +As you see the example above, we called `handler.RootHandler(w, r)` where the argument is filled by the `httptest.NewRecorder` and `http.NewRequest`. The recorder, will record whatever the response of the handler, you can read the body, status code, header, etc by only using `w.Result()`. Meanwhile the request will create a similar request like `http.NewRequest` with some hard-coded fields since `httptest.NewRequest` creates an incoming server request not like `http.NewRequest` that creates a client HTTP request which are usually used in `http.Client{}.Do` function, and that's it. Now you have the unit test for your HTTP handler. + +Thank you for reading! diff --git a/netlify.toml b/netlify.toml index 555e3f14..b979a999 100644 --- a/netlify.toml +++ b/netlify.toml @@ -3,10 +3,10 @@ publish = "docs" command = "echo 1" [context.production.environment] -HUGO_VERSION = "0.113.0" +HUGO_VERSION = "0.121.1" [context.deploy-preview.environment] -HUGO_VERSION = "0.113.0" +HUGO_VERSION = "0.121.1" [context.deploy-preview] command = "make netlify" diff --git a/themes/june b/themes/june index a8882e4a..de9cbe4f 160000 --- a/themes/june +++ b/themes/june @@ -1 +1 @@ -Subproject commit a8882e4abdd5172a09a626877085a11ebac7ce47 +Subproject commit de9cbe4f2751a674edb8142027359c3b5dba7210