gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 22:38:57 25 4
gpt4 key购买 nike

我对在Go 1.13中运行https://github.com/cavaliercoder/grab软件包的单元测试感兴趣。尽管我的GO111MODULE环境通常是on,但是我还是使用以下命令将软件包下载到了GOPATH:

env 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 test
panic: 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 +0x3a3
panic(0x132ba20, 0x1629ed0)
/usr/local/Cellar/go/1.13/libexec/src/runtime/panic.go:679 +0x1b2
github.com/cavaliercoder/grab.guessFilename(0xc00052aed0, 0xc0000aa008, 0x13951b5, 0x3, 0x139c440)
/Users/kurt/go/src/github.com/cavaliercoder/grab/util.go:51 +0x2d
github.com/cavaliercoder/grab.TestURLFilenames.func2(0xc00010ea00)
/Users/kurt/go/src/github.com/cavaliercoder/grab/util_test.go:54 +0x123
testing.tRunner(0xc00010ea00, 0x13afdf8)
/usr/local/Cellar/go/1.13/libexec/src/testing/testing.go:909 +0xc9
created by testing.(*T).Run
/usr/local/Cellar/go/1.13/libexec/src/testing/testing.go:960 +0x350
exit status 2
FAIL github.com/cavaliercoder/grab 2.531s

我查看了令人讨厌的测试文件 util_test.go,但找不到任何错误:
package grab

import (
"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",
}

for _, tc := range testCases {
req, _ := http.NewRequest("GET", tc, nil)
resp := &http.Response{
Request: req,
}
actual, err := guessFilename(resp)
if err != nil {
t.Errorf("%v", err)
}

if actual != expect {
t.Errorf("expected '%v', got '%v'", expect, actual)
}
}
})

t.Run("Invalid", func(t *testing.T) {
testCases := []string{
"http://test.com",
"http://test.com/",
"http://test.com/filename/",
"http://test.com/filename/?with=args",
"http://test.com/filename/#with-fragment",
"http://test.com/filename\x00",
}

for _, tc := range testCases {
req, _ := http.NewRequest("GET", tc, nil)
resp := &http.Response{
Request: req,
}

_, err := guessFilename(resp)
if err != ErrNoFilename {
t.Errorf("expected '%v', got '%v'", ErrNoFilename, err)
}
}
})
}

该错误消息似乎是在说 req是一个空指针,但是由于它是使用 http.NewRequest()定义的,所以我不知道怎么回事?

最佳答案

添加此错误打印:

            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

关于go - 运行抓取单元测试的“无效的内存地址或nil指针取消引用”?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58262202/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com