duai5344 2017-08-01 10:04
浏览 376

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

i have just started learning to write unit tests for the http requests, i went through several blogs but i didn't understood how to write tests for this using 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
", 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
}

I went through this blog and it exactly explains what my code requires but it uses Testing package and i want to implement this using ginkgo.

  • 写回答

1条回答 默认 最新

  • dongwen2162 2017-08-03 20:21
    关注

    Take a loot at ghttp: http://onsi.github.io/gomega/#ghttp-testing-http-clients https://godoc.org/github.com/onsi/gomega/ghttp

    A rough sketch might look like:

    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())
            })
        })
    })
    

    I think you've got a few issues with your code though. If you get a 500 or 503 status code you won't necessarily have an err so you'll need to create and send back a custom error.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?