duanpu8830 2019-02-15 15:01
浏览 66
已采纳

使用ExampleTest进行测试-预期输出不匹配

Running tool: /usr/local/go/bin/go test -timeout 30s -run ^(ExampleBuild)$

--- FAIL: ExampleBuild (0.00s)
got:
POST localhost/status?t=1 HTTP/1.1
Content-Type: application/json
want:
POST localhost/status?t=1 HTTP/1.1
Content-Type: application/json
FAIL
exit status 1

I'm trying to write test in go using the Example method. I have created a http request with header (Content-Type: application/json), query param t=1, method type POST and URL localhost.

The output in got: and want: looks to be the same, have also checked for whitespace characters. Not able to figure out what is the difference between these two here.

Not able to figure out what am I missing here.

import (
   "fmt"
   "net/http"
   "net/http/httputil"
)

func ExampleBuild() {

    req, err := http.NewRequest(http.MethodPost, "localhost/status?t=1", nil)
    req.Header.Add("content-type", "application/json")
    if err != nil {
        panic(err)
    }

    str, err := httputil.DumpRequest(req, false)
    if err != nil {
        panic(err)
    }
    fmt.Printf("%s", string(str))
    // Output:
    // POST localhost/status?t=1 HTTP/1.1
    // Content-Type: application/json

}

展开全部

  • 写回答

1条回答 默认 最新

  • dongyou5098 2019-02-15 15:28
    关注

    I think what's happening is that the HTTP header has for its line break. So this is what httputil.DumpRequest returns. But you're probably editing this file on a machine that doesn't use for line breaks, so the difference comes from there.

    A brute-force way to compare successfully would be:

    fmt.Println(strings.Replace(string(str), "", "", -1))
    

    Which removes the ""s from the string dumped by HTTP and it will compare successfully if your editor used only " " to break the expected output.

    A more elegant solution would depend on the specifics of your testing environment(s).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 vs 创建windows 窗体应用(.net framework)项目时,出现问题,无法进入下一步
  • ¥15 如何实现安卓Socks5代理服务端,并且实现内网穿透?
  • ¥50 自有服务器搭建正向代理及负载均衡应对高并发
  • ¥15 Expected a list, got: <class 'list'>. Correct! 为什么它不输出答案而是答案的类型
  • ¥15 pbootcms筛选怎么调用出来
  • ¥15 开发板和电机具体的参数?
  • ¥100 如何对超大数据分段并进行对比处理
  • ¥50 mmc在一个管理单元检测到错误
  • ¥15 如何正确使用pyside6,使其符合LGPL?
  • ¥15 百度网盘app文件浏览效果怎么做?
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部