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

问题描述 投票:0回答:1

我对在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()定义的,所以我不知道怎么回事?

go
1个回答
0
投票

添加此错误打印:

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

它将打印:

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

这意味着不允许使用此URL "http://test.com/filename\x00"

注释此行,然后起作用:

[p1gd0g@p1gd0g-pc grab]$ go test
PASS
ok      github.com/cavaliercoder/grab   2.586s
© www.soinside.com 2019 - 2024. All rights reserved.