No nonsense, a simplified httptest library

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Do not mention the library, to a net/http/httptest library to write general handler test method (source):

Package Handlersimport ("Net/http" "Net/http/httptest" "testing") Func Testhealthcheckhandler (t *testing. T) {//Create a request to pass to our handler.    We don ' t have any query parameters for now, so we'll//pass ' nil ' as the third parameter. Req, Err: = http. Newrequest ("GET", "/health-check", nil) if err! = Nil {t.fatal (ERR)}//We Create a Responserecorder (wh Ich satisfies http.    Responsewriter) to record the response. RR: = Httptest. Newrecorder () Handler: = http. Handlerfunc (Healthcheckhandler)//Our handlers satisfy HTTP.    Handler, so we can call their Servehttp method//directly and pass with our Request and Responserecorder. Handler.    Servehttp (RR, req)//Check The status code is what we expect. If status: = RR. Code; Status! = http. Statusok {T.errorf ("handler returned wrong status Code:got%v want%v", status, HTTP.    Statusok)}//Check The response body is what we expect. Expected: = ' {' Alive ': True} ' if RR. Body.string ()! = expected {T.errorf ("handler returned unexpected Body:got%v want%v", RR. Body.string (), expected)}}

It is simple to test a method with at least as many lines of code as a simple get request, and as for whether the request is to add parameters or not, it becomes another bigger problem.

Usage of the Library

A test that returns 400 forever Handlerfunc Badhandler (w http. Responsewriter, R *http. Request) {http. Error (W, "not a regular name or password", http. Statusbadrequest)}//Tests whether this handler returns 400NEW ("/bad", Badhandler, T). Do (). Checkcode (http. statusbadrequest)//test He is not returning 200 (of course will test failed) New ("/ok", Badhandler, T). Do (). Checkcode (http. Statusok)//With the header test new ("/", Badhandler, T). Post (). Addparams ("name", "Value1"). Addparams ("Nam22", "Value3"). Do ()//Take a cookie test and determine if the result contains a string. New ("/", Cookiehandler, T). Get (). Addcookies (Cookie). Do (). Bodycontains ("Testcookievalue")//Get *http. Responserecorder, and then test your own RR = New ("/dump", HeaderHandler, T). Post (). Addparams ("name", "Value1"). Do (). Responserecorder ()//Add parameter to request, do not write default is GET request new ("/ok", Badhandler, T). Addparams ("A", "AA"). Addparams ("b", "BB"). Do (). Checkcode (http. Statusok)//http Basic auth:new ("/bad", Badhandler, T). Setbasicauth (username, password). Do (). Checkcode (http. Statusbadrequest)//Custom HTTP. Request:new ("/bad", Badhandler, T). Setrequest (req). Do (). Checkcode (http. Statusbadrequest)//and mOre in test file and source code. 

Must have to be .Do() able to make the request, otherwise it will not be requested.
The check operation is to be preceded by the .Do() initialization operation .Do() .

Other

Library Address: https://github.com/qiuker521/...

The JSON test feature is added later.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.