dongmi6102 2016-05-10 16:15
浏览 25
已采纳

如何使用大猩猩上下文对中间件软件包进行单元测试

I have this net/http server setup with several middleware in a chain and I can't find examples on how I should test these...

I am using basic net/http with the gorilla/mux router and one Handle looks somewhat like this:

r.Handle("/documents", addCors(checkAPIKey(getDocuments(sendJSON)))).Methods("GET")

In these I aggregate some data and supply them via Gorilla Context context.Set methods.

Usually I test my http functions with httptest, and I hope to do it with these as well but I can't figure out how and I am curious as to what is the best way. Should I test each middleware seperately? Should I prefill the appropriate context values then when they are needed? Can I test this entire chain at once so I can just check desired states on input?

  • 写回答

1条回答 默认 最新

  • doujie9252 2016-05-11 00:02
    关注

    I would not test anything involving Gorilla or any other 3rd party package. If you want to test to make sure it works, i'd setup some external test runner or integration suite for the endpoints of a running version of your app (e.g. a C.I. server).

    Instead, test your Middleware and Handlers individually - as those you have control over.

    But, if you are set on testing the stack (mux -> handler -> handler -> handler -> MyHandler), this is where defining the middleware globally using functions as vars could help:

    var addCors = func(h http.Handler) http.Handler {
      ...
    }
    
    var checkAPIKey = func(h http.Handler) http.Handler {
      ...
    }
    

    During normal use, their implementation remains the same with change.

    r.Handle("/documents", addCors(checkAPIKey(getDocuments(sendJSON)))).Methods("GET")
    

    But for unit testing, you can override them:

    // important to keep the same package name for
    // your test file, so you can get to the private
    // vars.
    package main
    
    import (
      "testing"
    )
    
    func TestXYZHandler(t *testing.T) {
    
      // save the state, so you can restore at the end
      addCorsBefore := addCors
      checkAPIKeyBefore := checkAPIKey
    
      // override with whatever customization you want
      addCors = func(h http.Handler) http.Handler {
        return h
      }
      checkAPIKey = func(h http.Handler) http.Handler {
        return h
      }
    
      // arrange, test, assert, etc.
      //
    
      // when done, be a good dev and restore the global state
      addCors = addCorsBefore
      checkAPIKey = checkAPIKeyBefore
    }
    

    If you find yourself copy-n-pasting this boiler plate code often, move it to a global pattern within your unit tests:

    package main
    
    import (
      "testing"
    )
    
    var (
      addCorsBefore = addCors
      checkAPIKeyBefore = checkAPIKey
    )
    
    func clearMiddleware() {
      addCors = func(h http.Handler) http.Handler {
        return h
      }
      checkAPIKey = func(h http.Handler) http.Handler {
        return h
      }
    }
    
    func restoreMiddleware() {
      addCors = addCorsBefore
      checkAPIKey = checkAPIKeyBefore
    }
    
    func TestXYZHandler(t *testing.T) {
    
      clearMiddleware()
    
      // arrange, test, assert, etc.
      //
    
      restoreMiddleware()
    }
    

    A side note on unit testing end points...

    Since middleware should operate with sensible defaults (expected to pass normally and not mutex state of the underlying stream of data you want to test in func), I advise to unit test the middleware outside of the context of your actual main Handler function.

    That way, you have one set of unit tests strictly for your middleware. And another set of tests focusing purely on the primary Handler of the url you are calling. It makes discovering the code much easier for newcomers.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?