iis服务器助手广告
返回顶部
首页 > 资讯 > 后端开发 > GO >Golang:使用 httptest 拦截和模拟 HTTP 响应
  • 570
分享到

Golang:使用 httptest 拦截和模拟 HTTP 响应

2024-02-09 07:02:42 570人浏览 安东尼
摘要

PHP小编子墨为大家带来了一篇关于golang中使用Httptest拦截和模拟HTTP响应的文章。在这篇文章中,我们将深入探讨如何使用httptest包来进行HTTP请求的拦截和模拟响

PHP小编子墨为大家带来了一篇关于golang中使用Httptest拦截和模拟HTTP响应的文章。在这篇文章中,我们将深入探讨如何使用httptest包来进行HTTP请求的拦截和模拟响应,以便于进行单元测试和功能测试。通过使用httptest,我们可以轻松地模拟各种场景下的HTTP响应,进而测试我们的代码的正确性和鲁棒性。无论是对于初学者还是有经验的开发者来说,掌握这些技巧都是非常有用的。接下来,让我们一起来了解具体的实现方法吧!

问题内容

我研究了可用于 Golang 中模拟测试的各种不同工具,但我正在尝试使用 httptest 来完成此任务。特别是,我有一个这样的函数:

type contact struct {
  username string
  number int
}

func getResponse(c contact) string {
  url := fmt.Sprintf("https://myWEBsite/%s", c.username)
  req, err := http.NewRequest(http.MethodGet, url, nil)
  // error checking
 
  resp, err := http.DefaultClient.Do(req)
  // error checking
  
  return response
}

我读过的很多文档似乎都需要创建客户端接口或自定义传输。有没有办法在不更改此主代码的情况下模拟测试文件中的响应?我想将我的客户端、响应和所有相关详细信息保留在 getresponse 函数中。我可能有错误的想法,但我正在尝试找到一种方法来拦截 http.defaultclient.do(req) 调用并返回自定义响应,这可能吗?

解决方法

我读过似乎需要创建一个客户端界面

根本不改变这个主要代码

保持代码干净是一个很好的做法,您最终会习惯它,可测试的代码更干净,更干净的代码更可测试,所以不必担心更改您的代码(使用接口),这样它就可以接受模拟对象。

最简单形式的代码可以是这样的:

package main

import (
    "fmt"
    "net/http"
)

type contact struct {
    username string
    number   int
}

type client interface {
    do(req *http.request) (*http.response, error)
}

func main() {
    getresponse(http.defaultclient, contact{})
}

func getresponse(client client, c contact) string {
  url := fmt.sprintf("https://mywebsite/%s", c.username)
  req, _ := http.newrequest(http.methodget, url, nil)
  // error checking

  resp, _ := http.defaultclient.do(req)
  // error checking and response processing

  return response
}

你的测试可以是这样的:

package main

import (
    "net/http"
    "testing"
)

type mockclient struct {
}

// do function will cause mockclient to implement the client interface
func (tc mockclient) do(req *http.request) (*http.response, error) {
    return &http.response{}, nil
}

func testgetresponse(t *testing.t) {
    client := new(mockclient)
    getresponse(client, contact{})
}

但是如果您更喜欢使用 httptest:

package main

import (
    "fmt"
    "io"
    "net/http"
    "net/http/httptest"
)

type contact struct {
    username string
    number   int
}

func main() {
    fmt.Println(getResponse(contact{}))
}

func getResponse(c contact) string {
    // Make a test server
    ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintln(w, "your response")
    }))

    defer ts.Close()

    // You should still set your base url
    base_url := ts.URL

    url := fmt.Sprintf("%s/%s", base_url, c.username)
    req, _ := http.NewRequest(http.MethodGet, url, nil)

    // Use ts.Client() instead of http.DefaultClient in your tests.
    resp, _ := ts.Client().Do(req)

    // Processing the response
    response, _ := io.ReadAll(resp.Body)
    resp.Body.Close()

    return string(response)
}

以上就是Golang:使用 httptest 拦截和模拟 HTTP 响应的详细内容,更多请关注编程网其它相关文章!

您可能感兴趣的文档:

--结束END--

本文标题: Golang:使用 httptest 拦截和模拟 HTTP 响应

本文链接: https://www.lsjlt.com/news/562313.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作