How to test HTTP client in Golang?

by Devhubby on Oct 12, 2022 Health & Fitness 425 Views

You can use httptest.Server to test HTTP client in Golang. Below is the code as an example:

package crud_test

import (
   "net/http"
   "net/http/httptest"
   "testing"
)

func TestHttpClientWithHttpTest(t *testing.T) {
   // Use httptest to test http client
   server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
      // Verify request URL if needed
      equals(t, req.URL.String(), "/api/user")
      // Mock response
      rw.Write([]byte(`Success`))
   }))

   defer server.Close()

   // pass httptest client instead
   req := sendRequest{server.Client(), server.URL}
   body, err := req.DoStuff()

   ok(t, err)
   equals(t, []byte("Success"), body)
}

 

Article source: https://article-realm.com/article/Health-Fitness/29320-How-to-test-HTTP-client-in-Golang.html

URL

https://devhubby.com/thread/how-to-test-http-client-in-golang
How to test HTTP client in Golang?

Comments

No comments have been left here yet. Be the first who will do it.
Safety

captchaPlease input letters you see on the image.
Click on image to redraw.

Reviews

Guest

Overall Rating:

Statistics

Members
Members: 16750
Publishing
Articles: 78,448
Categories: 202
Online
Active Users: 4370
Members: 2
Guests: 4368
Bots: 9672
Visits last 24h (live): 9957
Visits last 24h (bots): 38911

Latest Comments

granny puts you inside a haunted house where survival depends on stealth and quick thinking. Explore abandoned rooms in search of useful items while avoiding the terrifying Granny, who reacts...
Every card revealed can either present an opportunity or force a complete shift in strategy. It is this blend of luck—inherent in the deal—and player skill that gives spider solitaire free its...
fnaf not only tests your courage but also keeps you in a constant state of vigilance. This style of psychological horror has allowed the game to carve out a unique identity and become an icon of...
For a high-end experience that combines beauty, elegance and sexual prowess, look no further than Noida Escort Service . Their stunning models are committed to providing a satisfying and discreet...
Orbit Beats is one of those games that's simple to understand but difficult to master.
Great write-up on the daily mechanics! One thing that always gets me is when a tricky puzzle threatens to end a long win streak. When I'm completely stuck and don't want to lose my progress,...
This list is useful for entrepreneurs comparing crypto exchange development companies in India, the USA, and the UK. When choosing a provider, review portfolio quality, security features,...
If you are looking for a top-tier Russian Delhi Call Girls Service , browse our verified profiles for the best experience.
on Jul 21, 2026 about Nice Girls Do It, Too!
When the demands of your schedule leave little room for error, booking a VIP Delhi Escorts Service becomes the most logical choice. True luxury is found in the sanctity of privacy, and these...
Stickman Hook is suitable for all ages, appealing to everyone from casual players to those who enjoy challenging their skills and chasing speed records.

Translate To: