douxiza9868 2019-02-27 13:42
浏览 70
已采纳

在Go中测试通过json http响应返回的错误

I'm currently working on an image handler in Go which is intended to reject the upload of unsupported files. My intentions are for the Go program to return an error back via the servers http.ResponceWritter detailing which case for rejection has been met, as json, for the upload service to use.

How errors are set up in the server code:

type Error struct {
    ImgHandlerError `json:"error"`
}

type ImgHandlerError struct {
    Message string `json:"message"`
    Code    string `json:"code"`
}

func MakeError(message, code string) *Error {
    errorMessage := &Error{
        ImgHandlerError: ImgHandlerError{
            Message: message,
            Code:    code,
        },
    }
    return errorMessage
}

func ThrowErr(errorMessage Error, writer http.ResponseWriter) {

    jData, err := json.Marshal(errorMessage)
       if err != nil  {
    }

    writer.Header().Set("Content-Type", "application/json")
    writer.WriteHeader(http.StatusForbidden)
    writer.Write(jData)

    return

}

How an error is generated and written as a HTTP response:

errorMes := *MakeError("PSD files are not supported", "DEF-IMG-0006")
ThrowErr(errorMes, res)

Currently this works as expected, for example when a .psd is uploaded the current HTTP response comes back as JSON.

{"error":{"message":"PSD files are not supported","code":"DEF-IMG-0006"}}

My problem is now testing this as I'm out of my depth, as this is my first Go program. I'm using the Ginko test suite. I've tried setting up the http.ResponseRecorder (as I think thats where the solution lies) in the test suite but I'm having no luck. Also of my current tests in the test suite only test functions which don't require the writer http.ResponseWriter as a param so I'm not sure if I need to start a server in the test suite to have a response writer to read from.

Any help is greatly appreciated

  • 写回答

1条回答 默认 最新

  • douanye8442 2019-02-27 14:10
    关注

    My problem is now testing this as I'm out of my depth, as this is my first Go program.

    So you want to do some end-to-end testing. In your test files you could do something like this.

    req := httptest.NewRequest("POST", "my/v1/endpoint", bytes.NewBuffer(payload))
    responseRecorder = httptest.NewRecorder()
    router.ServeHTTP(responseRecorder, request)
    

    Take a look at httptest.NewRequest and httptest.ResponseRecorder.

    Essentially you need to set up your router, personally I use mux and make requests to it just as you would if you were a real consumer of your API. Your responseRecorder after the request will have fields like Code for reading the response code and things like Body for reading the response body among other fields.

    e.g.

    if responseRecorder.Code != 200{
        t.FailNow()
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 MATLAB动图问题
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题