Trên GitHub, h2non/gock đã đạt 2.2k sao, nhóm API, ngôn ngữ Go. HTTP traffic mocking and testing made easy in Go ༼ʘ̚ل͜ʘ̚༽
Tóm tắt dựng từ metadata GitHub của chính dự án — chưa có bài review TopGit. Trang sẽ tự động cập nhật khi bài review đầy đủ được xuất bản.
VÌ SAO CHƯA CÓ REVIEW
TopGit viết bài đầy đủ cho repo có nhiều sao nhất và được yêu cầu nhiều nhất. Trang này là snapshot trong thời gian chờ — xem README gốc ở tab READ ME.
Versatile HTTP mocking made easy in Go that works with any net/http based stdlib implementation.
Heavily inspired by nock.
There is also its Python port, pook.
To get started, take a look to the examples.
Features
Simple, expressive, fluent API.
Semantic API DSL for declarative HTTP mock declarations.
Built-in helpers for easy JSON/XML mocking.
Supports persistent and volatile TTL-limited mocks.
Full regular expressions capable HTTP request mock matching.
Designed for both testing and runtime scenarios.
Match request by method, URL params, headers and bodies.
Extensible and pluggable HTTP matching rules.
Ability to switch between mock and real networking modes.
Ability to filter/map HTTP requests for accurate mock matching.
Supports map and filters to handle mocks easily.
Wide compatible HTTP interceptor using http.RoundTripper interface.
Works with any net/http compatible client, such as gentleman.
Network timeout/cancelation delay simulation.
Extensible and hackable API.
Dependency free.
Installation
go get -u github.com/h2non/gock
API
See godoc reference for detailed API documentation.
How it mocks
Intercepts any HTTP outgoing request via http.DefaultTransport or custom http.Transport used by any http.Client.
Matches outgoing HTTP requests against a pool of defined HTTP mock expectations in FIFO declaration order.
If at least one mock matches, it will be used in order to compose the mock HTTP response.
If no mock can be matched, it will resolve the request with an error, unless real networking mode is enable, in which case a real HTTP request will be performed.
Tips
Testing
Declare your mocks before you start declaring the concrete test logic:
func TestFoo(t *testing.T) {
defer gock.Off() // Flush pending mocks after test execution
gock.New("http://server.com").
Get("/bar").
Reply(200).
JSON(map[string]string{"foo": "bar"})
// Your test code starts here...
}
Race conditions
If you're running concurrent code, be aware that your mocks are declared first to avoid unexpected
race conditions while configuring gock or intercepting custom HTTP clients.
gock is not fully thread-safe, but sensible parts are.
Any help making gock more reliable in this sense is appreciated.
Define complex mocks first
If you're mocking a bunch of mocks in the same test suite, it's recommended to define the more
concrete mocks first, and then the generic ones.
This approach usually avoids matching unexpected generic mocks (e.g: specific header, body payload...) instead of the generic ones that performs less complex matches.
Disable gock traffic interception once done
In other to minimize potential side effects within your test code, it's a good practice
disabling gock once you are done with your HTTP testing logic.
A Go idiomatic approach for doing this can be using it in a defer statement, such as:
func TestGock (t *testing.T) {
defer gock.Off()
// ... my test code goes here
}
Intercept an http.Client just once
You don't need to intercept multiple times the same http.Client instance.
Just call gock.InterceptClient(client) once, typically at the beginning of your test scenarios.
Restore an http.Client after interception
NOTE: this is not required is you are using http.DefaultClient or http.DefaultTransport.
As a good testing pattern, you should call gock.RestoreClient(client) after running your test scenario, typically as after clean up hook.
You can also use a defer statement for doing it, as you do with gock.Off(), such as:
func TestGock (t *testing.T) {
defer gock.Off()
defer gock.RestoreClient(client)
// ... my test code goes here
}
Examples
See examples directory for more featured use cases.
package main
import (
"fmt"
"io/ioutil"
"net/http"
"github.com/h2non/gock"
)
func main() {
defer gock.Off()
defer gock.DisableNetworking()
gock.EnableNetworking()
gock.New("http://httpbin.org").
Get("/get").
Reply(201).
SetHeader("Server", "gock")
res, err := http.Get("http://httpbin.org/get")
if err != nil {
fmt.Errorf("Error: %s", err)
}
// The response status comes from the mock
fmt.Printf("Status: %d\n", res.StatusCode)
// The server header comes from mock as well
fmt.Printf("Server header: %s\n", res.Header.Get("Server"))
// Response body is the original
body, _ := ioutil.ReadAll(res.Body)
fmt.Printf("Body: %s", string(body))
}
Trang TopGit này là một snapshot — tab "Readme" hiển thị nguyên văn README của repo (đã bỏ link, giữ ảnh). Repo GitHub ở github.com/h2non/gock là nguồn chính thức.
h2non/gock có bao nhiêu sao?
h2non/gock có 2.2k sao GitHub — tải lại trang để xem số mới nhất, hoặc xem trực tiếp github.com/h2non/gock. TopGit phản chiếu số sao của GitHub nhưng không cam kết đến từng phút.
h2non/gock có phải mã nguồn mở không?
Có — h2non/gock phát hành theo license MIT, nghĩa là mã nguồn mở để đọc, fork và (tùy license) tái sử dụng. Mã: github.com/h2non/gock.
h2non/gock còn đang phát triển không?
Commit gần nhất trên h2non/gock là 1.9 năm trước (theo timestamp GitHub). Repo có 112 fork — một chỉ báo về mức độ quan tâm của cộng đồng.
h2non/gock là gì?
h2non/gock (h2non/gock) là dự án Go trên GitHub. Theo mô tả gốc: HTTP traffic mocking and testing made easy in Go ༼ʘ̚ل͜ʘ̚༽
h2non/gock so với các dự án API khác thế nào?
h2non/gock được TopGit xếp vào nhóm API, với 2.2k sao GitHub và viết bằng Go. Xem trang chủ đề API trên TopGit để so sánh với các dự án tương tự theo số sao và mức độ hoạt động.
Đọc đầy đủ README ở tab phía trên.
gock có đáng để bạn bỏ thời gian?
ChatGPT, Claude và Perplexity đều đọc được trang này. Hỏi thử xem họ nghĩ gì về gock.