为了账号安全,请及时绑定邮箱和手机立即绑定

运行抓取单元测试时“无效的内存地址或零指针取消引用”?

运行抓取单元测试时“无效的内存地址或零指针取消引用”?

Go
米脂 2023-07-26 19:48:54
我有兴趣在 Go 1.13 中运行https://github.com/cavaliercoder/grab包的单元测试。虽然我的GO111MODULE环境通常是,但我已经使用以下命令on将包下载到我的:GOPATHenv GO111MODULE=off go get -u -d github.com/cavaliercoder/grab在结果grab目录中,我运行了go mod init以下命令go.mod:module github.com/cavaliercoder/grab go 1.13现在,如果我尝试运行go test,我会收到以下错误:> go testpanic: runtime error: invalid memory address or nil pointer dereference [recovered]    panic: runtime error: invalid memory address or nil pointer dereference[signal SIGSEGV: segmentation violation code=0x1 addr=0x10 pc=0x12cc70d]goroutine 1556 [running]:testing.tRunner.func1(0xc00010ea00)    /usr/local/Cellar/go/1.13/libexec/src/testing/testing.go:874 +0x3a3panic(0x132ba20, 0x1629ed0)    /usr/local/Cellar/go/1.13/libexec/src/runtime/panic.go:679 +0x1b2github.com/cavaliercoder/grab.guessFilename(0xc00052aed0, 0xc0000aa008, 0x13951b5, 0x3, 0x139c440)    /Users/kurt/go/src/github.com/cavaliercoder/grab/util.go:51 +0x2dgithub.com/cavaliercoder/grab.TestURLFilenames.func2(0xc00010ea00)    /Users/kurt/go/src/github.com/cavaliercoder/grab/util_test.go:54 +0x123testing.tRunner(0xc00010ea00, 0x13afdf8)    /usr/local/Cellar/go/1.13/libexec/src/testing/testing.go:909 +0xc9created by testing.(*T).Run    /usr/local/Cellar/go/1.13/libexec/src/testing/testing.go:960 +0x350exit status 2FAIL    github.com/cavaliercoder/grab   2.531s我查看了有问题的测试文件,util_test.go但找不到任何问题:package grabimport (    "fmt"    "net/http"    "net/url"    "testing")func TestURLFilenames(t *testing.T) {    t.Run("Valid", func(t *testing.T) {        expect := "filename"        testCases := []string{            "http://test.com/filename",            "http://test.com/path/filename",            "http://test.com/deep/path/filename",            "http://test.com/filename?with=args",            "http://test.com/filename#with-fragment",            "http://test.com/filename?with=args&and#with-fragment",        }错误消息似乎说这req是一个空指针,但由于它是使用定义的,所以http.NewRequest()我不明白这是怎么回事?
查看完整描述

1 回答

?
慕村225694

TA贡献1880条经验 获得超4个赞

添加此错误打印:

            req, err1 := http.NewRequest("GET", tc, nil)
                        if err1 != nil {
                log.Println(err1.Error())
            }

它会打印:

解析http://test.com/filename : net/url: URL 中的控制字符无效

这意味着该网址"http://test.com/filename\x00"不被允许。

注释掉这一行然后它就可以工作了:

[p1gd0g@p1gd0g-pc grab]$ go test
PASS
ok      github.com/cavaliercoder/grab   2.586s


查看完整回答
反对 回复 2023-07-26
  • 1 回答
  • 0 关注
  • 214 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号