关于 golang 编写学习过程中遇到需要测试函数功能时,如何做的疑问

关于golang编写学习过程中遇到需要测试标准库函数功能时,如何做的疑问

在学习golang时,或者编写代码的时候,我会遇到一些没用过的标准库函数。通常我会查看标准库文档和用法,接着是测试。那么问题来了,我通常需要为了测试一个功能,比如net/http库中的SetBasicAuth这个函数。
如果我要测试它的功能,那么我需要写如下代码:(包含了完整的功能来测试)

func Curlgithubauth() {
    var url string = "https://api.github.com"
    var result githubjson
    var name string = "foot"
    var passwd string = "bar"
    client := &http.Client{}
    req, err := http.NewRequest("GET", url, nil)
    req.SetBasicAuth(name, passwd)
    res, err := client.Do(req)
    if err != nil {
        log.Fatal(err)
    }
    defer res.Body.Close()
    err = json.NewDecoder(res.Body).Decode(&result)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(result.Message)
    fmt.Println(result.Documentation_url)
    fmt.Println(req.Header.Get("Authorization"))
}

来进行测试,我感觉这样太慢了,不知道大家都是用的什么办法测试这样的功能的。有没有更好的办法,请指点一下,谢谢!

taadis
最佳答案

SetBasicAuth 函数的本质是在请求头, 也就是 request header 里添加指定的数据, 你可以在调用 SetBasicAuth 函数后, 验证下 request header 里的指定键值是不是你设置的值即可. :relaxed:

对应的 header 为: Authorization

你可以自己取出来对比. 不过计算要稍微麻烦一点.

当然 Go 做的比你想象的更好, 提供了 SetBasicAuth() 函数的同时, 也提供了 BasicAuth() 函数, 让你方便设置和获取对应的数据.

BasicAuth() 函数的签名如下:

// request.go
// BasicAuth returns the username and password provided in the request's
// Authorization header, if the request uses HTTP Basic Authentication.
// See RFC 2617, Section 2.
func (r *Request) BasicAuth() (username, password  string, ok bool) {
    auth := r.Header.Get("Authorization")
    if auth == "" {
    return
    }
    return parseBasicAuth(auth)
}

测试代码如下:

// http_test.go
package http

import (
    "net/http"
    "testing"
)

// 测试 SetBasicAuth 函数是否有用
func TestSetBasicAuth(t *testing.T) {
    req, err := http.NewRequest("GET", "", nil)
    if err != nil {
        t.Fatal(err)
        t.FailNow()
    }
    req.SetBasicAuth("name", "123456")
    username, password, ok := req.BasicAuth()
    if !ok || username != "name" || password != "123456" {
        t.Errorf("test  fail")
    }
}

然后运行测试

go test -v http_test.go

不出意外的话, 应该输出如下

=== RUN   TestSetBasicAuth
--- PASS: TestSetBasicAuth (0.00s)
PASS
ok      command-line-arguments    2.610s
4年前 评论
叶小萌 (楼主) 4年前
讨论数量: 2
taadis

SetBasicAuth 函数的本质是在请求头, 也就是 request header 里添加指定的数据, 你可以在调用 SetBasicAuth 函数后, 验证下 request header 里的指定键值是不是你设置的值即可. :relaxed:

对应的 header 为: Authorization

你可以自己取出来对比. 不过计算要稍微麻烦一点.

当然 Go 做的比你想象的更好, 提供了 SetBasicAuth() 函数的同时, 也提供了 BasicAuth() 函数, 让你方便设置和获取对应的数据.

BasicAuth() 函数的签名如下:

// request.go
// BasicAuth returns the username and password provided in the request's
// Authorization header, if the request uses HTTP Basic Authentication.
// See RFC 2617, Section 2.
func (r *Request) BasicAuth() (username, password  string, ok bool) {
    auth := r.Header.Get("Authorization")
    if auth == "" {
    return
    }
    return parseBasicAuth(auth)
}

测试代码如下:

// http_test.go
package http

import (
    "net/http"
    "testing"
)

// 测试 SetBasicAuth 函数是否有用
func TestSetBasicAuth(t *testing.T) {
    req, err := http.NewRequest("GET", "", nil)
    if err != nil {
        t.Fatal(err)
        t.FailNow()
    }
    req.SetBasicAuth("name", "123456")
    username, password, ok := req.BasicAuth()
    if !ok || username != "name" || password != "123456" {
        t.Errorf("test  fail")
    }
}

然后运行测试

go test -v http_test.go

不出意外的话, 应该输出如下

=== RUN   TestSetBasicAuth
--- PASS: TestSetBasicAuth (0.00s)
PASS
ok      command-line-arguments    2.610s
4年前 评论
叶小萌 (楼主) 4年前

file
file

我觉得源代码里面的注释写得很清楚了,意思就是说给请求添加基本的auth字段,http包封装了一层好用的接口。我觉得可以选一个好一点的ide,支持定义跳转的,比如goland,vscode应该也可以,读一读注释,就能大致了解用途了。希望有帮到你。

4年前 评论
叶小萌 (楼主) 4年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!