gpt4 book ai didi

unit-testing - 如何使用 Ginkgo 对 HTTP 请求进行单元测试?

转载 作者:数据小太阳 更新时间:2023-10-29 03:04:18 25 4
gpt4 key购买 nike

我刚开始学习为 http 请求编写单元测试,我浏览了几个博客,但我不明白如何使用 Ginkgo 为此编写测试。

func getVolDetails(volName string, obj interface{}) error {
addr := os.Getenv("SOME_ADDR")
if addr == "" {
err := errors.New("SOME_ADDR environment variable not set")
fmt.Println(err)
return err
}
url := addr + "/path/to/somepage/" + volName
client := &http.Client{
Timeout: timeout,
}
resp, err := client.Get(url)
if resp != nil {
if resp.StatusCode == 500 {
fmt.Printf("VSM %s not found\n", volName)
return err
} else if resp.StatusCode == 503 {
fmt.Println("server not reachable")
return err
}
} else {
fmt.Println("server not reachable")
return err
}

if err != nil {
fmt.Println(err)
return err
}
defer resp.Body.Close()

return json.NewDecoder(resp.Body).Decode(obj)
}

// GetVolAnnotations gets annotations of volume
func GetVolAnnotations(volName string) (*Annotations, error) {
var volume Volume
var annotations Annotations
err := getVolDetails(volName, &volume)
if err != nil || volume.Metadata.Annotations == nil {
if volume.Status.Reason == "pending" {
fmt.Println("VSM status Unknown to server")
}
return nil, err
}
// Skipped some part,not required
}

我经历了这个blog它准确地解释了我的代码需要什么,但它使用了测试包,我想使用 ginkgo 来实现它。

最佳答案

看看 ghttp 包: http://onsi.github.io/gomega/#ghttp-testing-http-clients https://godoc.org/github.com/onsi/gomega/ghttp

粗略的草图可能是这样的:

import (
"os"

. "github.com/onsi/ginkgo/tmp"
"github.com/onsi/gomega/ghttp"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var _ = Describe("GetVolAnnotations", func() {
var server *ghttp.Server
var returnedVolume Volume
var statusCode int

BeforeEach(func() {
server = ghttp.NewServer()
os.Setenv("SOME_ADDR", server.Addr())

server.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("GET", "/path/to/somepage/VOLUME"),
ghttp.RespondWithJSONEncodedPtr(&statusCode, &returnedVolume),
)
)
})

AfterEach(func() {
server.Close()
})

Context("When when the server returns a volume", func() {
BeforeEach(func() {
returnedVolume = Volume{
Metadata: Metadata{
Annotations: []string{"foo"}
}
}
statusCode = 200
})

It("returns the annotations associated with the volume", func() {
Expect(GetVolAnnotations("VOLUME")).To(Equal([]string{"foo"}))
})
})

Context("when the server returns 500", func() {
BeforEach(func() {
statusCode = 500
})

It("errors", func() {
value, err := GetVolAnnotations("VOLUME")
Expect(value).To(BeNil())
Expect(err).To(HaveOccurred())
})
})

Context("when the server returns 503", func() {
BeforEach(func() {
statusCode = 503
})

It("errors", func() {
value, err := GetVolAnnotations("VOLUME")
Expect(value).To(BeNil())
Expect(err).To(HaveOccurred())
})
})
})

不过我认为您的代码存在一些问题。如果您收到 500 或 503 状态代码,则不一定有 err,因此您需要创建自定义错误并从服务器发回。

关于unit-testing - 如何使用 Ginkgo 对 HTTP 请求进行单元测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45434849/

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