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

我有兴趣在 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",

        }

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


米脂
浏览 215回答 1
1回答

慕村225694

添加此错误打印:            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
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go