【golang】28、用 httptest 做 web server 的 controller 的单测

2024-03-12 13:12

本文主要是介绍【golang】28、用 httptest 做 web server 的 controller 的单测,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

  • 一、构建 HTTP server
    • 1.1 model.go
    • 1.2 server.go
    • 1.3 curl 验证 server 功能
      • 1.3.1 新建
      • 1.3.2 查询
      • 1.3.3 更新
      • 1.3.4 删除
  • 二、httptest 测试
    • 2.1 完整示例
    • 2.2 实现逻辑
    • 2.3 其他示例
    • 2.4 用 TestMain 避免重复的测试代码
    • 2.5 gin 框架的 httptest

一、构建 HTTP server

1.1 model.go

package mainimport ("errors""time"
)var TopicCache = make([]*Topic, 0, 16)type Topic struct {Id        int       `json:"id"`Title     string    `json:"title"`Content   string    `json:"content"`CreatedAt time.Time `json:"created_at"`
}// 从数组中找到一项, 根据 id 找到数组的下标
func FindTopic(id int) (*Topic, error) {if err := checkIndex(id); err != nil {return nil, err}return TopicCache[id-1], nil
}// 创建一个 Topic 实例, 没有输入参数, 内部根据 Topic 数组的长度来确定新 Topic 的 id
func (t *Topic) Create() error {// 初始时len 为 0, id 为 1, 即数组下标为0时并不放置元素, 而数组从下标为1才开始放置元素t.Id = len(TopicCache) + 1 // 忽略用户传入的 id, 而是根据数组的长度, 决定此项的 Idt.CreatedAt = time.Now()TopicCache = append(TopicCache, t) // 初始时数组为空, 放入的第一个元素是 Id = 1return nil
}// 更新一个 Topic 实例, 通过 id 找到数组下标, 最终改的还是数组里的值
func (t *Topic) Update() error {if err := checkIndex(t.Id); err != nil {return err}TopicCache[t.Id-1] = treturn nil
}func (t *Topic) Delete() error {if err := checkIndex(t.Id); err != nil {return err}TopicCache[t.Id-1] = nilreturn nil
}func checkIndex(id int) error {if id > 0 && len(TopicCache) <= id-1 {return errors.New("The topic is not exists!")}return nil
}

1.2 server.go

package mainimport ("encoding/json""net/http""path""strconv"
)func main() {http.HandleFunc("/topic/", handleRequest)http.ListenAndServe(":2017", nil)
}// main handler function
func handleRequest(w http.ResponseWriter, r *http.Request) {var err errorswitch r.Method {case http.MethodGet:err = handleGet(w, r)case http.MethodPost:err = handlePost(w, r)case http.MethodPut:err = handlePut(w, r)case http.MethodDelete:err = handleDelete(w, r)}if err != nil {http.Error(w, err.Error(), http.StatusInternalServerError)return}
}// 获取一个帖子
// 如 GET /topic/1
func handleGet(w http.ResponseWriter, r *http.Request) error {// 用户输入的 url 中有 id, 通过 path.Base(r.URL.Path) 获取 idid, err := strconv.Atoi(path.Base(r.URL.Path))if err != nil {return err}topic, err := FindTopic(id)if err != nil {return err}// 序列化结果并输出output, err := json.MarshalIndent(&topic, "", "\t\t")if err != nil {return err}w.Header().Set("Content-Type", "application/json")w.Write(output)return nil
}// 增加一个帖子
// POST /topic/
func handlePost(w http.ResponseWriter, r *http.Request) (err error) {// 构造长度为 r.ContentLength 的缓冲区body := make([]byte, r.ContentLength)// 读取到缓冲区r.Body.Read(body)// 反序列化到对象var topic = new(Topic)err = json.Unmarshal(body, &topic)if err != nil {return}// 执行操作err = topic.Create()if err != nil {return}w.WriteHeader(http.StatusOK)return
}// 更新一个帖子
// PUT /topic/1
func handlePut(w http.ResponseWriter, r *http.Request) error {id, err := strconv.Atoi(path.Base(r.URL.Path))if err != nil {return err}topic, err := FindTopic(id)if err != nil {return err}body := make([]byte, r.ContentLength)r.Body.Read(body)json.Unmarshal(body, topic)err = topic.Update()if err != nil {return err}w.WriteHeader(http.StatusOK)return nil
}// 删除一个帖子
// DELETE /topic/1
func handleDelete(w http.ResponseWriter, r *http.Request) (err error) {id, err := strconv.Atoi(path.Base(r.URL.Path))if err != nil {return}topic, err := FindTopic(id)if err != nil {return}err = topic.Delete()if err != nil {return}w.WriteHeader(http.StatusOK)return
}

1.3 curl 验证 server 功能

1.3.1 新建

curl -i -X POST http://localhost:2017/topic/ -H 'content-type: application/json' -d '{"title":"a", "content":"b"}'HTTP/1.1 200 OK
Date: Mon, 11 Mar 2024 02:54:08 GMT
Content-Length: 0

1.3.2 查询

curl -i -X GET http://localhost:2017/topic/1HTTP/1.1 200 OK
Content-Type: application/json
Date: Mon, 11 Mar 2024 03:00:11 GMT
Content-Length: 99{"id": 1,"title": "a","content": "b","created_at": "2024-03-11T10:59:44.043029+08:00"
}

1.3.3 更新

curl -i -X PUT http://localhost:2017/topic/1 -H 'content-type: application/json' -d '{"title": "c", "content": "d"}'HTTP/1.1 200 OK
Date: Mon, 11 Mar 2024 03:01:51 GMT
Content-Length: 0
curl -i -X GET http://localhost:2017/topic/1     HTTP/1.1 200 OK
Content-Type: application/json
Date: Mon, 11 Mar 2024 03:01:54 GMT
Content-Length: 99{"id": 1,"title": "c","content": "d","created_at": "2024-03-11T10:59:44.043029+08:00"
}

1.3.4 删除

curl -i -X DELETE http://localhost:2017/topic/1HTTP/1.1 200 OK
Date: Mon, 11 Mar 2024 03:03:41 GMT
Content-Length: 0
curl -i -X GET http://localhost:2017/topic/1   
HTTP/1.1 200 OK
Content-Type: application/json
Date: Mon, 11 Mar 2024 03:04:27 GMT
Content-Length: 4null

二、httptest 测试

上文,通过 curl 自测了 controller,现在通过 net/http/httptest 测试,这种测试方式其实是没有 HTTP 调用的,是通过将 handler() 函数绑定到 url 上实现的。

2.1 完整示例

package mainimport ("net/http""net/http/httptest""strings""testing"
)func TestHandlePost(t *testing.T) {// mux 是多路复用器的意思mux := http.NewServeMux()mux.HandleFunc("/topic/", handleRequest) // 将 [业务的 handleRequest() 函数] 注册到 mux 的 /topic/ 路由上// 构造一个请求reader := strings.NewReader(`{"title":"e", "content":"f"}`)r, _ := http.NewRequest(http.MethodPost, "/topic/", reader)// 构造一个响应 (httptest.ResponseRecorder 实现了 http.ResponseWriter 接口)w := httptest.NewRecorder()mux.ServeHTTP(w, r)//handleRequest(w, r)// 获取响应结果resp := w.Result()if resp.StatusCode != http.StatusOK {t.Errorf("Expected status OK; got %v", resp.Status)}
}

2.2 实现逻辑

实现逻辑如下:
首先配置路由,将 /topic 的请求都路由给 handleRequest() 函数实现。

mux := http.NewServeMux()
mux.HandleFunc("/topic/", handleRequest)

因为 handleRequest(w http.ResponseWriter, r *http.Request) 函数的签名是 w 和 r 两个参数,所以为了测试,需要构造这两个参数实例。

因为 httptest.ResponseRecorder 实现了 http.ResponseWriter 接口,所以可以用 httptest.NewRecorder() 表示 w。

准备好之后,就可以执行了

  • 可以只调用 handleRequest(w, r)
  • 也可以调用 mux.ServeHTTP(w, r),其内部也会调用 handleRequest(w, r),这会更完整的测试整个流程。

最后,通过 go test -v 可以执行测试。

$ go test -v       
=== RUN   TestHandlePost
--- PASS: TestHandlePost (0.00s)
PASS
ok      benchmarkdemo   0.095s

2.3 其他示例

func TestHandleGet(t *testing.T) {mux := http.NewServeMux()mux.HandleFunc("/topic/", handleRequest)r, _ := http.NewRequest(http.MethodGet, "/topic/1", nil)w := httptest.NewRecorder()mux.ServeHTTP(w, r)resp := w.Result()if resp.StatusCode != http.StatusOK {t.Errorf("Expected status OK; got %v", resp.Status)}topic := new(Topic)json.Unmarshal(w.Body.Bytes(), topic)if topic.Id != 1 {t.Errorf("cannot get topic by id")}
}

注意,因为数据没有落地存储,为了保证后面的测试正常,请将 TestHandlePost 放在最前面。

  • 如果 go test -v 测试整个包的话,TestHandlePost 和 TestHandleGet 两个单测都能成功
  • 但如果分开测试的话,只有 TestHandlePost 能成功,而 TestHandleGet 会失败(因为没有 POST 创建流程,而只有 GET 创建流程的话,在业务逻辑的数组中,找不到 id = 1 的项,就会报错)

2.4 用 TestMain 避免重复的测试代码

细心的朋友应该会发现,上面的测试代码有重复,比如:

mux := http.NewServeMux()
mux.HandleFunc("/topic/", handleRequest)

以及:

w := httptest.NewRecorder()

这正好是前面学习的 setup 可以做的事情,因此可以使用 TestMain 来做重构。实现如下:

var w *httptest.ResponseRecorderfunc TestMain(m *testing.M) {w = httptest.NewRecorder()os.Exit(m.Run())
}

2.5 gin 框架的 httptest

package serviceimport ("fmt""log""net/http""net/http/httptest""strings""testing""github.com/gin-gonic/gin"
)type userINfo struct {ID   uint64 `json:"id"`Name string `json:"name"`
}func handler(c *gin.Context) {var info userINfoif err := c.ShouldBindJSON(&info); err != nil {log.Panic(err)}fmt.Println(info)c.Writer.Write([]byte(`{"status": 200}`))
}func TestHandler(t *testing.T) {rPath := "/user"router := gin.Default()router.GET(rPath, handler)req, _ := http.NewRequest("GET", rPath, strings.NewReader(`{"id": "1","name": "joe"}`))w := httptest.NewRecorder()router.ServeHTTP(w, req)t.Logf("status: %d", w.Code)t.Logf("response: %s", w.Body.String())
}

这篇关于【golang】28、用 httptest 做 web server 的 controller 的单测的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/801366

相关文章

SQL Server跟踪自动统计信息更新实战指南

《SQLServer跟踪自动统计信息更新实战指南》本文详解SQLServer自动统计信息更新的跟踪方法,推荐使用扩展事件实时捕获更新操作及详细信息,同时结合系统视图快速检查统计信息状态,重点强调修... 目录SQL Server 如何跟踪自动统计信息更新:深入解析与实战指南 核心跟踪方法1️⃣ 利用系统目录

全面解析Golang 中的 Gorilla CORS 中间件正确用法

《全面解析Golang中的GorillaCORS中间件正确用法》Golang中使用gorilla/mux路由器配合rs/cors中间件库可以优雅地解决这个问题,然而,很多人刚开始使用时会遇到配... 目录如何让 golang 中的 Gorilla CORS 中间件正确工作一、基础依赖二、错误用法(很多人一开

SQL Server 中的 WITH (NOLOCK) 示例详解

《SQLServer中的WITH(NOLOCK)示例详解》SQLServer中的WITH(NOLOCK)是一种表提示,等同于READUNCOMMITTED隔离级别,允许查询在不获取共享锁的情... 目录SQL Server 中的 WITH (NOLOCK) 详解一、WITH (NOLOCK) 的本质二、工作

SQL Server安装时候没有中文选项的解决方法

《SQLServer安装时候没有中文选项的解决方法》用户安装SQLServer时界面全英文,无中文选项,通过修改安装设置中的国家或地区为中文中国,重启安装程序后界面恢复中文,解决了问题,对SQLSe... 你是不是在安装SQL Server时候发现安装界面和别人不同,并且无论如何都没有中文选项?这个问题也

Python Web框架Flask、Streamlit、FastAPI示例详解

《PythonWeb框架Flask、Streamlit、FastAPI示例详解》本文对比分析了Flask、Streamlit和FastAPI三大PythonWeb框架:Flask轻量灵活适合传统应用... 目录概述Flask详解Flask简介安装和基础配置核心概念路由和视图模板系统数据库集成实际示例Stre

SQL server数据库如何下载和安装

《SQLserver数据库如何下载和安装》本文指导如何下载安装SQLServer2022评估版及SSMS工具,涵盖安装配置、连接字符串设置、C#连接数据库方法和安全注意事项,如混合验证、参数化查... 目录第一步:打开官网下载对应文件第二步:程序安装配置第三部:安装工具SQL Server Manageme

C#连接SQL server数据库命令的基本步骤

《C#连接SQLserver数据库命令的基本步骤》文章讲解了连接SQLServer数据库的步骤,包括引入命名空间、构建连接字符串、使用SqlConnection和SqlCommand执行SQL操作,... 目录建议配合使用:如何下载和安装SQL server数据库-CSDN博客1. 引入必要的命名空间2.

golang程序打包成脚本部署到Linux系统方式

《golang程序打包成脚本部署到Linux系统方式》Golang程序通过本地编译(设置GOOS为linux生成无后缀二进制文件),上传至Linux服务器后赋权执行,使用nohup命令实现后台运行,完... 目录本地编译golang程序上传Golang二进制文件到linux服务器总结本地编译Golang程序

golang版本升级如何实现

《golang版本升级如何实现》:本文主要介绍golang版本升级如何实现问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录golanwww.chinasem.cng版本升级linux上golang版本升级删除golang旧版本安装golang最新版本总结gola

SQL Server配置管理器无法打开的四种解决方法

《SQLServer配置管理器无法打开的四种解决方法》本文总结了SQLServer配置管理器无法打开的四种解决方法,文中通过图文示例介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录方法一:桌面图标进入方法二:运行窗口进入检查版本号对照表php方法三:查找文件路径方法四:检查 S