├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── body.go ├── body_test.go ├── client.go ├── client_test.go ├── const.go ├── doc.go ├── example ├── basic_mock_client.go ├── basic_mock_client_test.go ├── glide.lock └── glide.yaml ├── go.mod ├── go.sum ├── request.go ├── request_test.go ├── response.go ├── response_test.go └── server_test.go /.gitignore: -------------------------------------------------------------------------------- 1 | vendor/ 2 | example/vendor/ 3 | *.coverprofile 4 | coverage.* 5 | .idea/ -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: go # 声明构建语言环境 2 | sudo: false # 开启基于容器的Travis CI任务,让编译效率更高。 3 | 4 | notifications: # 每次构建的时候是否通知,如果不想收到通知,那就设置false吧(email: false) 5 | email: 6 | recipients: 7 | - hexileee@gmail.com 8 | on_success: change 9 | on_failure: always 10 | 11 | go: 12 | - "1.11" 13 | 14 | install: 15 | - go build 16 | - go get github.com/mattn/goveralls 17 | 18 | 19 | script: # 集成脚本 20 | - go test -v -cover -coverprofile=htest.coverprofile 21 | - goveralls -coverprofile=htest.coverprofile -service=travis-ci -repotoken $COVERALLS_TOKEN 22 | 23 | after_success: 24 | - bash <(curl -s https://codecov.io/bash) 25 | 26 | env: #env环境变量设置,travis提供的repo_token安全方式 27 | global: 28 | secure: "n/DHx2qPJy4GDZrQXxhEhzpZsWlWJMOpj1+NKBHMDhXN4UkurVMJLPTbwhCfoEpq63qt2a+KU9AxehrXXsqcdnfT4Emq+r1xCP1aXJXYhyw9NEhHzkZjSLGY7kRh83hqpwat1lPx+dzfP1omrc8ducxAFmckL0/CZKBZoOoeGZ1gXAQaDB2GZnzKtn4L1vhn7ppFIn3oy1GMYjlO/dOHJ4HY8Y18HKXpSDnQezPRRh8MkPMKRORhn3HDwfk1zZVh4CYjVswxo8MvwZjXuCXkZaDkp0J6dmar2LMnxNI1tqou92Bt4XFfWDivvakcKFQfrxCoHCXcloAO2AetG/VNSnUgtMA/8CKZ6uQkGdbXvcYJexX9VvJTVaijCwaULiu+L8KaGh2Re1R3qQuI4fd1HUe3cwtvbnWxQiOMhrtYu9UyUR87UpXeaxPg2eGAVY/nW0T0VLhBhK4nvDqitYCDNK+nS42z35ZyH8jKaMCuh7r/GbYvmjQBwbiQaJvFGde9GXNsJ4c4Zvpt18JyrG88iFkXobYFVl4PWo0neEDfexznms0iDmWlccg+1Gb7qhMMf6VL2H0y8CUC9vaw8Y1qzUT6Lp4Xy7CvbsaBz+0Mmy/c93FqW0Y12NNYXZ5I7bQoCDrjXl1GiSPx+CyssGk4ROIeSwipRn4V+mTa3+/N8dU=" 29 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 ZJU QSC 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## htest is a http-test package 2 | 3 | [![Coverage Status](https://coveralls.io/repos/github/Hexilee/htest/badge.svg)](https://coveralls.io/github/Hexilee/htest) 4 | [![Go Report Card](https://goreportcard.com/badge/github.com/Hexilee/htest)](https://goreportcard.com/report/github.com/Hexilee/htest) 5 | [![Build Status](https://travis-ci.org/Hexilee/htest.svg?branch=master)](https://travis-ci.org/Hexilee/htest) 6 | [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://github.com/Hexilee/htest/blob/master/LICENSE) 7 | [![Documentation](https://godoc.org/github.com/Hexilee/htest?status.svg)](https://godoc.org/github.com/Hexilee/htest) 8 | 9 | 10 | Table of Contents 11 | ================= 12 | 13 | * [Basic Usage](#basic-usage) 14 | * [Test MockServer](#test-mockserver) 15 | * [Test HandlerFunc](#test-handlerfunc) 16 | * [To ServeMux](#to-servemux) 17 | * [To Echo](#to-echo) 18 | * [Test RealServer](#test-realserver) 19 | * [Github API](#github-api) 20 | * [Client](#client) 21 | * [Set MockServer](#set-mockserver) 22 | * [HandlerFunc](#handlerfunc) 23 | * [Handler](#handler) 24 | * [Construct Request](#construct-request) 25 | * [Http Methods](#http-methods) 26 | * [Request](#request) 27 | * [Set Headers](#set-headers) 28 | * [Add Cookie](#add-cookie) 29 | * [Test](#test) 30 | * [Send](#send) 31 | * [As http.Request](#as-httprequest) 32 | * [Response](#response) 33 | * [Assert StatusCode](#assert-statuscode) 34 | * [Code](#code) 35 | * [StatusXXX](#statusxxx) 36 | * [Assert Headers](#assert-headers) 37 | * [Headers](#headers) 38 | * [HeaderXXX](#headerxxx) 39 | * [Assert Body](#assert-body) 40 | * [Get Body](#get-body) 41 | * [Bind Body](#bind-body) 42 | * [Body Types](#body-types) 43 | * [As http.Response](#as-httpresponse) 44 | * [Body](#body) 45 | * [JSON](#json) 46 | * [Assert JSON Key](#assert-json-key) 47 | * [Assert JSON Empty or Not](#assert-json-empty-or-not) 48 | * [Bind JSON](#bind-json) 49 | * [XML](#xml) 50 | * [MD5](#md5) 51 | * [Assert MD5 Hash](#assert-md5-hash) 52 | * [Get MD5 Hash value](#get-md5-hash-value) 53 | * [SHA1](#sha1) 54 | * [Appendix](#appendix) 55 | * [consts](#consts) 56 | 57 | 58 | ### Basic Usage 59 | 60 | ----------------- 61 | 62 | #### Test MockServer 63 | 64 | > Test a Handler or a HandlerFunc 65 | 66 | ##### Test HandlerFunc 67 | 68 | ```go 69 | // example/basic_mock_client.go 70 | package myapp 71 | 72 | import ( 73 | "io" 74 | "net/http" 75 | ) 76 | 77 | func NameHandler(w http.ResponseWriter, req *http.Request) { 78 | io.WriteString(w, `{"name": "hexi"}`) 79 | } 80 | ``` 81 | 82 | ```go 83 | // example/basic_mock_client_test.go 84 | package myapp 85 | 86 | import ( 87 | "testing" 88 | "github.com/Hexilee/htest" 89 | ) 90 | 91 | func TestNameHandlerFunc(t *testing.T) { 92 | htest.NewClient(t). 93 | ToFunc(NameHandler). 94 | Get(""). 95 | Test(). 96 | StatusOK(). 97 | JSON(). 98 | String("name", "hexi") 99 | } 100 | ``` 101 | 102 | You can also test handler (*http.ServeMux, *echo.Echo .etc.) 103 | 104 | ##### To ServeMux 105 | 106 | ```go 107 | // example/basic_mock_client.go 108 | package myapp 109 | 110 | import ( 111 | "io" 112 | "net/http" 113 | ) 114 | 115 | var ( 116 | Mux *http.ServeMux 117 | ) 118 | 119 | func init() { 120 | Mux = http.NewServeMux() 121 | Mux.HandleFunc("/name", NameHandler) 122 | } 123 | 124 | func NameHandler(w http.ResponseWriter, req *http.Request) { 125 | io.WriteString(w, `{"name": "hexi"}`) 126 | } 127 | ``` 128 | 129 | ```go 130 | // example/basic_mock_client_test.go 131 | package myapp 132 | 133 | import ( 134 | "testing" 135 | "github.com/Hexilee/htest" 136 | ) 137 | 138 | func TestNameHandler(t *testing.T) { 139 | htest.NewClient(t). 140 | To(Mux). 141 | Get("/name"). 142 | Test(). 143 | StatusOK(). 144 | JSON(). 145 | String("name", "hexi") 146 | } 147 | ``` 148 | 149 | ##### To Echo 150 | 151 | ```go 152 | // example/basic_mock_client.go 153 | package myapp 154 | 155 | import ( 156 | "io" 157 | "github.com/labstack/echo" 158 | ) 159 | 160 | var ( 161 | server *echo.Echo 162 | ) 163 | 164 | func init() { 165 | server = echo.New() 166 | server.GET("/name", NameHandlerEcho) 167 | } 168 | 169 | func NameHandlerEcho(c echo.Context) error { 170 | return c.String(http.StatusOK, `{"name": "hexi"}`) 171 | } 172 | ``` 173 | 174 | ```go 175 | // example/basic_mock_client_test.go 176 | package myapp 177 | 178 | import ( 179 | "testing" 180 | "github.com/Hexilee/htest" 181 | ) 182 | 183 | func TestNameHandlerEcho(t *testing.T) { 184 | htest.NewClient(t). 185 | To(server). 186 | Get("/name"). 187 | Test(). 188 | StatusOK(). 189 | JSON(). 190 | String("name", "hexi") 191 | } 192 | ``` 193 | 194 | #### Test RealServer 195 | 196 | > Send a http request and test the response 197 | 198 | ##### Github API 199 | 200 | ```go 201 | // request_test.go 202 | func TestRequest_Send(t *testing.T) { 203 | NewClient(t). 204 | Get("https://api.github.com/users/Hexilee"). 205 | Send(). 206 | StatusOK(). 207 | JSON(). 208 | String("login", "Hexilee") 209 | } 210 | ``` 211 | 212 | 213 | ### Client 214 | 215 | ------- 216 | 217 | #### Set MockServer 218 | 219 | > Set mock server to be tested (Do not need it when you test real server) 220 | 221 | ##### HandlerFunc 222 | 223 | > Set a HandlerFunc as mock server 224 | 225 | ```go 226 | // example/basic_mock_client_test.go 227 | package myapp 228 | 229 | import ( 230 | "testing" 231 | "github.com/Hexilee/htest" 232 | ) 233 | 234 | func TestNameHandlerFunc(t *testing.T) { 235 | htest.NewClient(t). 236 | ToFunc(NameHandler). 237 | Get(""). 238 | Test(). 239 | StatusOK(). 240 | JSON(). 241 | String("name", "hexi") 242 | } 243 | ``` 244 | 245 | ##### Handler 246 | 247 | > Set a Handler as mock server 248 | 249 | ```go 250 | // example/basic_mock_client_test.go 251 | package myapp 252 | 253 | import ( 254 | "testing" 255 | "github.com/Hexilee/htest" 256 | ) 257 | 258 | func TestNameHandler(t *testing.T) { 259 | htest.NewClient(t). 260 | To(Mux). 261 | Get("/name"). 262 | Test(). 263 | StatusOK(). 264 | JSON(). 265 | String("name", "hexi") 266 | } 267 | ``` 268 | 269 | #### Construct Request 270 | 271 | > Construct htest.Request using different http methods 272 | 273 | ##### Http Methods 274 | 275 | > For example 276 | 277 | - Get 278 | 279 | ```go 280 | // client.go 281 | func (c Client) Get(path string) *Request 282 | ``` 283 | 284 | > More 285 | 286 | - Head 287 | - Trace 288 | - Options 289 | - Connect 290 | - Delete 291 | - Post 292 | - Put 293 | - Patch 294 | 295 | ### Request 296 | 297 | ------- 298 | 299 | #### Set Headers 300 | 301 | > Set headers and return *Request for chaining-call 302 | 303 | - SetHeader 304 | 305 | ```go 306 | // server_test.go 307 | 308 | Mux.Get("/request/header", HeaderHandler) 309 | 310 | // request_test.go 311 | 312 | func HeaderHandler(w http.ResponseWriter, req *http.Request) { 313 | if req.Header.Get(HeaderContentType) == MIMEApplicationJSON { 314 | io.WriteString(w, `{"result": "JSON"}`) 315 | return 316 | } 317 | http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) 318 | } 319 | 320 | func TestRequest_SetHeader(t *testing.T) { 321 | client := NewClient(t).To(Mux) 322 | // bad content type 323 | client. 324 | Get("/request/header"). 325 | SetHeader(HeaderContentType, MIMEApplicationForm). 326 | Test(). 327 | StatusBadRequest() 328 | 329 | // right 330 | client. 331 | Get("/request/header"). 332 | SetHeader(HeaderContentType, MIMEApplicationJSON). 333 | Test(). 334 | StatusOK(). 335 | JSON(). 336 | String("result", "JSON") 337 | } 338 | ``` 339 | 340 | > HeaderContentType, MIMEApplicationForm are constants in const.go 341 | > For more information, you can refer to [Appendix](#appendix) 342 | 343 | 344 | - SetHeaders 345 | 346 | ```go 347 | // request_test.go 348 | 349 | func TestRequest_SetHeaders(t *testing.T) { 350 | client := NewClient(t).To(Mux) 351 | // bad content type 352 | client.Get("/request/header"). 353 | SetHeaders( 354 | map[string]string{ 355 | HeaderContentType: MIMEApplicationForm, 356 | }, 357 | ). 358 | Test(). 359 | StatusBadRequest() 360 | 361 | // right 362 | client.Get("/request/header"). 363 | SetHeaders( 364 | map[string]string{ 365 | HeaderContentType: MIMEApplicationJSON, 366 | }, 367 | ). 368 | Test(). 369 | StatusOK(). 370 | JSON(). 371 | String("result", "JSON") 372 | } 373 | ``` 374 | 375 | #### Add Cookie 376 | 377 | > Add cookie and return *Request for chaining-call 378 | 379 | ```go 380 | // server_test.go 381 | 382 | Mux.Get("/request/cookie", CookieHandler) 383 | 384 | // request_test.go 385 | 386 | var ( 387 | testCookie = http.Cookie{Name: "test_cookie", Value: "cookie_value"} 388 | ) 389 | 390 | func CookieHandler(w http.ResponseWriter, req *http.Request) { 391 | cookie, err := req.Cookie(testCookie.Name) 392 | if err != nil { 393 | http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden) 394 | return 395 | } 396 | io.WriteString(w, fmt.Sprintf(`{"cookie": "%s"}`, cookie)) 397 | } 398 | 399 | 400 | func TestRequest_AddCookie(t *testing.T) { 401 | client := NewClient(t). 402 | To(Mux) 403 | client. 404 | Get("/request/cookie"). 405 | Test(). 406 | StatusForbidden() 407 | client. 408 | Get("/request/cookie"). 409 | AddCookie(&testCookie). 410 | Test(). 411 | StatusOK(). 412 | JSON(). 413 | String("cookie", testCookie.String()) 414 | } 415 | ``` 416 | 417 | #### Test 418 | 419 | > Calling *Request.Test will test the mock server and return a *Response. 420 | 421 | > You must have called Client.To or Client.ToFunc, otherwise causing a panic (htest.MockNilError) 422 | 423 | ```go 424 | // request_test.go 425 | 426 | func TestRequest_Test(t *testing.T) { 427 | defer func() { 428 | assert.Equal(t, MockNilError, recover()) 429 | }() 430 | 431 | NewClient(t). 432 | Get("/request/header"). 433 | SetHeader(HeaderContentType, MIMEApplicationForm). 434 | Test(). 435 | StatusBadRequest() 436 | } 437 | ``` 438 | 439 | #### Send 440 | 441 | > Calling *Request.Send will send a real http request and return a *Response 442 | 443 | ```go 444 | // request_test.go 445 | 446 | func TestRequest_Send(t *testing.T) { 447 | NewClient(t). 448 | Get("https://api.github.com/users/Hexilee"). 449 | Send(). 450 | StatusOK(). 451 | JSON(). 452 | String("login", "Hexilee") 453 | } 454 | 455 | ``` 456 | 457 | #### As http.Request 458 | 459 | > As *http.Request is embedded in htest.Request, you can regard *htest.Request as *http.Request. Just like: 460 | 461 | ```go 462 | userAgent := NewClient(t). 463 | Get("https://api.github.com/users/Hexilee"). 464 | UserAgent() 465 | 466 | ``` 467 | 468 | 469 | ### Response 470 | 471 | ------- 472 | 473 | #### Assert StatusCode 474 | 475 | Assert Response.StatusCode 476 | 477 | ##### Code 478 | 479 | > *Response.Code(statusCode int) 480 | 481 | ```go 482 | // response_test.go 483 | 484 | var ( 485 | ResponseCodeServer = chi.NewRouter() 486 | ) 487 | 488 | func init() { 489 | ResponseCodeServer.Get("/response/statusCode/{code}", StatusHandler) 490 | } 491 | 492 | func StatusHandler(w http.ResponseWriter, req *http.Request) { 493 | codeStr := chi.URLParam(req, "code") 494 | code, err := strconv.Atoi(codeStr) 495 | if err != nil { 496 | w.WriteHeader(http.StatusNotFound) 497 | return 498 | } 499 | w.WriteHeader(code) 500 | } 501 | 502 | func TestResponse_Code(t *testing.T) { 503 | NewClient(t). 504 | To(ResponseCodeServer). 505 | Get(fmt.Sprintf("/response/statusCode/%d", http.StatusBadRequest)). 506 | Test(). 507 | Code(http.StatusBadRequest) 508 | } 509 | ``` 510 | 511 | 512 | ##### StatusXXX 513 | 514 | > For more ergonomic development, *htest.Response has many methods to assert all the StatusCode in net/http 515 | 516 | ```go 517 | // response_test.go 518 | 519 | func TestResponse_StatusContinue(t *testing.T) { 520 | NewClient(t). 521 | To(ResponseCodeServer). 522 | Get(fmt.Sprintf("/response/statusCode/%d", http.StatusContinue)). 523 | Test(). 524 | StatusContinue() 525 | } 526 | 527 | ``` 528 | 529 | #### Assert Headers 530 | 531 | Assert Response.Headlers 532 | 533 | ##### Headers 534 | 535 | > *Response.Headers(key, expect string) 536 | 537 | ```go 538 | // response_test.go 539 | 540 | var ( 541 | ResponseHeadersServer = chi.NewRouter() 542 | ) 543 | 544 | func init() { 545 | ResponseHeadersServer.Get("/response/headers", HeadersHandler) 546 | } 547 | 548 | func HeadersHandler(w http.ResponseWriter, req *http.Request) { 549 | query := req.URL.Query() 550 | header := query.Get("header") 551 | value := query.Get("value") 552 | w.Header().Set(header, value) 553 | } 554 | 555 | func TestResponse_Headers(t *testing.T) { 556 | url := fmt.Sprintf("/response/headers?header=%s&value=%s", HeaderContentType, MIMEApplicationJSON) 557 | NewClient(t). 558 | To(ResponseHeadersServer). 559 | Get(url). 560 | Test(). 561 | Headers(HeaderContentType, MIMEApplicationJSON) 562 | } 563 | ``` 564 | 565 | ##### HeaderXXX 566 | 567 | > For more ergonomic development, *htest.Response has many methods to assert all the Headers in const.go 568 | 569 | 570 | ```go 571 | // response_test.go 572 | 573 | func TestResponse_HeaderAccept(t *testing.T) { 574 | url := fmt.Sprintf("/response/headers?header=%s&value=%s", HeaderAccept, "htest") 575 | NewClient(t). 576 | To(ResponseHeadersServer). 577 | Get(url). 578 | Test(). 579 | HeaderAccept("htest") 580 | } 581 | ``` 582 | 583 | #### Assert Body 584 | 585 | You can assert data in body straightly. 586 | 587 | ```go 588 | // server_test.go 589 | Mux.Get("/body/user", UserDataHandler) 590 | 591 | func UserDataHandler(w http.ResponseWriter, req *http.Request) { 592 | io.WriteString(w, UserData) 593 | } 594 | 595 | // response_test.go 596 | 597 | const ( 598 | UserData = `{ 599 | "id": 1, 600 | "name": "hexi" 601 | }` 602 | ) 603 | 604 | 605 | 606 | func TestResponse_Expect(t *testing.T) { 607 | NewClient(t). 608 | To(Mux). 609 | Get("/body/user"). 610 | Test(). 611 | StatusOK(). 612 | Expect(UserData) 613 | } 614 | ``` 615 | 616 | 617 | #### Get Body 618 | 619 | You can get data in body straightly 620 | 621 | - String 622 | 623 | ```go 624 | // response_test.go 625 | 626 | func TestResponse_String(t *testing.T) { 627 | assert.Equal(t, UserData, NewClient(t). 628 | To(Mux). 629 | Get("/body/user"). 630 | Test(). 631 | StatusOK(). 632 | String()) 633 | } 634 | ``` 635 | 636 | - Bytes 637 | 638 | ```go 639 | // response_test.go 640 | 641 | func TestResponse_Bytes(t *testing.T) { 642 | assert.Equal(t, []byte(UserData), NewClient(t). 643 | To(Mux). 644 | Get("/body/user"). 645 | Test(). 646 | StatusOK(). 647 | Bytes()) 648 | } 649 | 650 | ``` 651 | #### Bind Body 652 | 653 | If type of data in body is JSON, you can unmarshal it straightly 654 | 655 | ```go 656 | // response_test.go 657 | 658 | type ( 659 | User struct { 660 | Id uint 661 | Name string 662 | } 663 | ) 664 | 665 | func TestResponse_Bind(t *testing.T) { 666 | user := new(User) 667 | NewClient(t). 668 | To(Mux). 669 | Get("/body/user"). 670 | Test(). 671 | StatusOK(). 672 | Bind(user) 673 | assert.Equal(t, user.Id, uint(1)) 674 | assert.Equal(t, user.Name, "hexi") 675 | } 676 | ``` 677 | 678 | #### Body Types 679 | 680 | You can return data in 4 types 681 | 682 | * [JSON](#json) 683 | * [XML](#xml) 684 | * [MD5](#md5) 685 | * [SHA1](#sha1) 686 | 687 | 688 | #### As http.Response 689 | 690 | > As *http.Response is embedded in htest.Response, you can regard *htest.Response as *http.Response. Just like: 691 | 692 | ```go 693 | 694 | assert.Equal(t, "HTTP/1.1", NewClient(t). 695 | To(Mux). 696 | Get("/body/user"). 697 | Test(). 698 | Proto 699 | ) 700 | 701 | ``` 702 | 703 | ### Body 704 | 705 | htest provide 4 types of data to be returned 706 | 707 | #### JSON 708 | 709 | data as JSON 710 | 711 | ##### Assert JSON Key 712 | 713 | - Exist(key string) 714 | - NotExist(key string) 715 | - String(key, expect string) 716 | - Int(key string, expect int64) 717 | - True(key string) 718 | - False(key string) 719 | - Uint(key string, expect uint64) 720 | - Time(key string, expect time.Time) 721 | - Float(key string, expect float64) 722 | 723 | ```go 724 | // body_test.go 725 | 726 | func TestJSON_Exist(t *testing.T) { 727 | NewClient(t). 728 | To(Mux). 729 | Get("/name"). 730 | Test(). 731 | StatusOK(). 732 | JSON(). 733 | Exist("name"). 734 | NotExist("stuid") 735 | } 736 | 737 | ``` 738 | 739 | ```go 740 | func TestJSON_String(t *testing.T) { 741 | user := new(User) 742 | NewClient(t). 743 | To(Mux). 744 | Get("/body/user"). 745 | Test(). 746 | StatusOK(). 747 | JSON(). 748 | String("name", "hexi) 749 | } 750 | ``` 751 | 752 | ##### Assert JSON Empty or Not 753 | 754 | ```go 755 | func TestJSON_NotEmpty(t *testing.T) { 756 | user := new(User) 757 | NewClient(t). 758 | To(Mux). 759 | Get("/body/user"). 760 | Test(). 761 | StatusOK(). 762 | JSON(). 763 | NotEmpty() 764 | } 765 | ``` 766 | 767 | ##### Bind JSON 768 | 769 | ```go 770 | // body_test.go 771 | 772 | type ( 773 | User struct { 774 | Id uint 775 | Name string 776 | } 777 | ) 778 | 779 | func TestJSON_Bind(t *testing.T) { 780 | user := new(User) 781 | NewClient(t). 782 | To(Mux). 783 | Get("/body/user"). 784 | Test(). 785 | StatusOK(). 786 | JSON(). 787 | Bind(user) 788 | assert.Equal(t, user.Id, uint(1)) 789 | assert.Equal(t, user.Name, "hexi") 790 | } 791 | ``` 792 | 793 | #### XML 794 | 795 | Same as JSON. 796 | 797 | For more examples, you can find them in body_test.go 798 | 799 | #### MD5 800 | 801 | ##### Assert MD5 Hash 802 | 803 | ```go 804 | // body_test.go 805 | 806 | func TestMD5_Expect(t *testing.T) { 807 | NewClient(t). 808 | To(Mux). 809 | Get("/body/user"). 810 | Test(). 811 | StatusOK(). 812 | MD5(). 813 | Expect(UserDataMD5) 814 | } 815 | ``` 816 | 817 | ##### Get MD5 Hash value 818 | 819 | ```go 820 | hash := NewClient(t). 821 | To(Mux). 822 | Get("/body/user"). 823 | Test(). 824 | StatusOK(). 825 | MD5(). 826 | Body() 827 | ``` 828 | 829 | #### SHA1 830 | 831 | Same as MD5. 832 | 833 | For more examples, you can find them in body_test.go 834 | 835 | 836 | ### Appendix 837 | 838 | #### consts 839 | 840 | There are many constants of header or header value in const.go 841 | 842 | ```go 843 | // const.go 844 | 845 | package htest 846 | 847 | // HTTP methods 848 | const ( 849 | CONNECT = "CONNECT" 850 | DELETE = "DELETE" 851 | GET = "GET" 852 | HEAD = "HEAD" 853 | OPTIONS = "OPTIONS" 854 | PATCH = "PATCH" 855 | POST = "POST" 856 | PUT = "PUT" 857 | TRACE = "TRACE" 858 | ) 859 | 860 | // MIME types 861 | const ( 862 | MIMEApplicationJSON = "application/json" 863 | MIMEApplicationJSONCharsetUTF8 = MIMEApplicationJSON + "; " + charsetUTF8 864 | MIMEApplicationJavaScript = "application/javascript" 865 | MIMEApplicationJavaScriptCharsetUTF8 = MIMEApplicationJavaScript + "; " + charsetUTF8 866 | MIMEApplicationXML = "application/xml" 867 | MIMEApplicationXMLCharsetUTF8 = MIMEApplicationXML + "; " + charsetUTF8 868 | MIMETextXML = "text/xml" 869 | MIMETextXMLCharsetUTF8 = MIMETextXML + "; " + charsetUTF8 870 | MIMEApplicationForm = "application/x-www-form-urlencoded" 871 | MIMEApplicationProtobuf = "application/protobuf" 872 | MIMEApplicationMsgpack = "application/msgpack" 873 | MIMETextHTML = "text/html" 874 | MIMETextHTMLCharsetUTF8 = MIMETextHTML + "; " + charsetUTF8 875 | MIMETextPlain = "text/plain" 876 | MIMETextPlainCharsetUTF8 = MIMETextPlain + "; " + charsetUTF8 877 | MIMEMultipartForm = "multipart/form-data" 878 | MIMEOctetStream = "application/octet-stream" 879 | ) 880 | 881 | const ( 882 | charsetUTF8 = "charset=UTF-8" 883 | ) 884 | 885 | // Headers 886 | const ( 887 | HeaderAccept = "Accept" 888 | HeaderAcceptEncoding = "Accept-Encoding" 889 | HeaderAllow = "Allow" 890 | HeaderAuthorization = "Authorization" 891 | HeaderContentDisposition = "Content-Disposition" 892 | HeaderContentEncoding = "Content-Encoding" 893 | HeaderContentLength = "Content-Length" 894 | HeaderContentType = "Content-Type" 895 | HeaderCookie = "Cookie" 896 | HeaderSetCookie = "Set-Cookie" 897 | HeaderIfModifiedSince = "If-Modified-Since" 898 | HeaderLastModified = "Last-Modified" 899 | HeaderLocation = "Location" 900 | HeaderUpgrade = "Upgrade" 901 | HeaderVary = "Vary" 902 | HeaderWWWAuthenticate = "WWW-Authenticate" 903 | HeaderXForwardedFor = "X-Forwarded-For" 904 | HeaderXForwardedProto = "X-Forwarded-Proto" 905 | HeaderXForwardedProtocol = "X-Forwarded-Protocol" 906 | HeaderXForwardedSsl = "X-Forwarded-Ssl" 907 | HeaderXUrlScheme = "X-Url-Scheme" 908 | HeaderXHTTPMethodOverride = "X-HTTP-Method-Override" 909 | HeaderXRealIP = "X-Real-IP" 910 | HeaderXRequestID = "X-Request-ID" 911 | HeaderServer = "Server" 912 | HeaderOrigin = "Origin" 913 | 914 | // Access control 915 | HeaderAccessControlRequestMethod = "Access-Control-Request-Method" 916 | HeaderAccessControlRequestHeaders = "Access-Control-Request-Headers" 917 | HeaderAccessControlAllowOrigin = "Access-Control-Allow-Origin" 918 | HeaderAccessControlAllowMethods = "Access-Control-Allow-Methods" 919 | HeaderAccessControlAllowHeaders = "Access-Control-Allow-Headers" 920 | HeaderAccessControlAllowCredentials = "Access-Control-Allow-Credentials" 921 | HeaderAccessControlExposeHeaders = "Access-Control-Expose-Headers" 922 | HeaderAccessControlMaxAge = "Access-Control-Max-Age" 923 | 924 | // Security 925 | HeaderStrictTransportSecurity = "Strict-Transport-Security" 926 | HeaderXContentTypeOptions = "X-Content-Type-Options" 927 | HeaderXXSSProtection = "X-XSS-Protection" 928 | HeaderXFrameOptions = "X-Frame-Options" 929 | HeaderContentSecurityPolicy = "Content-Security-Policy" 930 | HeaderXCSRFToken = "X-CSRF-Token" 931 | ) 932 | 933 | 934 | ``` 935 | 936 | 937 | 938 | 939 | -------------------------------------------------------------------------------- /body.go: -------------------------------------------------------------------------------- 1 | package htest 2 | 3 | import ( 4 | "bytes" 5 | "encoding/json" 6 | "encoding/xml" 7 | "github.com/basgys/goxml2json" 8 | "github.com/stretchr/testify/assert" 9 | "github.com/tidwall/gjson" 10 | "io/ioutil" 11 | "testing" 12 | "time" 13 | ) 14 | 15 | type ( 16 | JSON struct { 17 | body []byte 18 | *testing.T 19 | } 20 | 21 | XML struct { 22 | *JSON 23 | body []byte 24 | } 25 | 26 | MD5 struct { 27 | body []byte 28 | *testing.T 29 | } 30 | 31 | SHA1 struct { 32 | body []byte 33 | *testing.T 34 | } 35 | ) 36 | 37 | func NewJSON(body []byte, t *testing.T) *JSON { 38 | return &JSON{ 39 | body: body, 40 | T: t, 41 | } 42 | } 43 | 44 | func NewXML(body []byte, t *testing.T) *XML { 45 | jsonBuf, _ := xml2json.Convert(bytes.NewBuffer(body)) 46 | jsonBody, _ := ioutil.ReadAll(jsonBuf) 47 | return &XML{ 48 | body: body, 49 | JSON: NewJSON(jsonBody, t), 50 | } 51 | } 52 | 53 | func NewMD5(body []byte, t *testing.T) *MD5 { 54 | return &MD5{ 55 | body: body, 56 | T: t, 57 | } 58 | } 59 | 60 | func NewSHA1(body []byte, t *testing.T) *SHA1 { 61 | return &SHA1{ 62 | body: body, 63 | T: t, 64 | } 65 | } 66 | 67 | func (j *JSON) GetKey(key string) (result gjson.Result, exist bool) { 68 | result = gjson.GetBytes(j.body, key) 69 | exist = result.Exists() 70 | return 71 | } 72 | 73 | func (j *JSON) Exist(key string) *JSON { 74 | _, exist := j.GetKey(key) 75 | assert.True(j.T, exist) 76 | return j 77 | } 78 | 79 | func (j *JSON) NotExist(key string) *JSON { 80 | _, exist := j.GetKey(key) 81 | assert.False(j.T, exist) 82 | return j 83 | } 84 | 85 | func (j *JSON) String(key, expect string) *JSON { 86 | result, _ := j.GetKey(key) 87 | assert.Equal(j.T, expect, result.String()) 88 | return j 89 | } 90 | 91 | func (j *JSON) Int(key string, expect int64) *JSON { 92 | result, _ := j.GetKey(key) 93 | assert.Equal(j.T, expect, result.Int()) 94 | return j 95 | } 96 | 97 | func (j *JSON) True(key string) *JSON { 98 | result, _ := j.GetKey(key) 99 | assert.True(j.T, result.Bool()) 100 | return j 101 | } 102 | 103 | func (j *JSON) False(key string) *JSON { 104 | result, _ := j.GetKey(key) 105 | assert.False(j.T, result.Bool()) 106 | return j 107 | } 108 | 109 | func (j *JSON) Uint(key string, expect uint64) *JSON { 110 | result, _ := j.GetKey(key) 111 | assert.Equal(j.T, expect, result.Uint()) 112 | return j 113 | } 114 | 115 | func (j *JSON) Time(key string, expect time.Time) *JSON { 116 | result, _ := j.GetKey(key) 117 | assert.Equal(j.T, expect, result.Time()) 118 | return j 119 | } 120 | 121 | func (j *JSON) Float(key string, expect float64) *JSON { 122 | result, _ := j.GetKey(key) 123 | assert.Equal(j.T, expect, result.Float()) 124 | return j 125 | } 126 | 127 | func (j *JSON) Empty() *JSON { 128 | body := bytes.Trim(j.Body(), "\"\n") 129 | assert.Equal(j.T, "", string(body)) 130 | return j 131 | } 132 | 133 | func (j *JSON) NotEmpty() *JSON { 134 | body := bytes.Trim(j.Body(), "\"\n") 135 | assert.NotEqual(j.T, "", string(body)) 136 | return j 137 | } 138 | 139 | func (j *JSON) Body() []byte { 140 | return j.body 141 | } 142 | 143 | func (j *JSON) Bind(obj interface{}) error { 144 | return json.Unmarshal(j.body, obj) 145 | } 146 | 147 | func (x *XML) Exist(key string) *XML { 148 | x.JSON.Exist(key) 149 | return x 150 | } 151 | 152 | func (x *XML) NotExist(key string) *XML { 153 | x.JSON.NotExist(key) 154 | return x 155 | } 156 | func (x *XML) String(key, expect string) *XML { 157 | x.JSON.String(key, expect) 158 | return x 159 | } 160 | 161 | func (x *XML) Int(key string, expect int64) *XML { 162 | x.JSON.Int(key, expect) 163 | return x 164 | } 165 | 166 | func (x *XML) True(key string) *XML { 167 | x.JSON.True(key) 168 | return x 169 | } 170 | 171 | func (x *XML) False(key string) *XML { 172 | x.JSON.False(key) 173 | return x 174 | } 175 | 176 | func (x *XML) Uint(key string, expect uint64) *XML { 177 | x.JSON.Uint(key, expect) 178 | return x 179 | } 180 | 181 | func (x *XML) Time(key string, expect time.Time) *XML { 182 | x.JSON.Time(key, expect) 183 | return x 184 | } 185 | 186 | func (x *XML) Float(key string, expect float64) *XML { 187 | x.JSON.Float(key, expect) 188 | return x 189 | } 190 | 191 | func (x *XML) Empty() *XML { 192 | assert.Equal(x.T, "", string(x.Body())) 193 | return x 194 | } 195 | 196 | func (x *XML) NotEmpty() *XML { 197 | assert.NotEqual(x.T, "", string(x.Body())) 198 | return x 199 | } 200 | 201 | func (x *XML) Body() []byte { 202 | return x.body 203 | } 204 | 205 | func (x *XML) Bind(obj interface{}) error { 206 | return xml.Unmarshal(x.body, obj) 207 | } 208 | 209 | func (m *MD5) Expect(expect string) *MD5 { 210 | assert.Equal(m.T, expect, string(m.Body())) 211 | return m 212 | } 213 | 214 | func (m *MD5) Body() []byte { 215 | return m.body 216 | } 217 | 218 | func (s *SHA1) Expect(expect string) *SHA1 { 219 | assert.Equal(s.T, expect, string(s.Body())) 220 | return s 221 | } 222 | 223 | func (s *SHA1) Body() []byte { 224 | return s.body 225 | } 226 | -------------------------------------------------------------------------------- /body_test.go: -------------------------------------------------------------------------------- 1 | package htest 2 | 3 | import ( 4 | "github.com/stretchr/testify/assert" 5 | "io" 6 | "net/http" 7 | "testing" 8 | "time" 9 | ) 10 | 11 | const ( 12 | WrongXMLData = ` 13 | 14 | 15 | 1 16 | hexi 17 | ` 18 | 19 | JSONAssertData = ` 20 | { 21 | "number": 1, 22 | "time": "2018-02-22T00:00:00Z", 23 | "ok": true, 24 | "no": false, 25 | } 26 | ` 27 | XMLAssertData = ` 28 | 29 | 1 30 | 31 | true 32 | false 33 | 34 | ` 35 | 36 | AssertDataTimeStr = "2018-02-22T00:00:00Z" 37 | ) 38 | 39 | var ( 40 | AssertDataTime, _ = time.Parse(time.RFC3339, AssertDataTimeStr) 41 | ) 42 | 43 | type ( 44 | AssertStruct struct { 45 | Number int `json:"number" xml:"Number"` 46 | Time time.Time `json:"time" xml:"Time"` 47 | OK bool `json:"ok" xml:"OK"` 48 | NO bool `json:"no" xml:"NO"` 49 | } 50 | ) 51 | 52 | func TestJSON_Exist(t *testing.T) { 53 | NewClient(t). 54 | To(Mux). 55 | Get("/name"). 56 | Test(). 57 | StatusOK(). 58 | JSON(). 59 | Exist("name"). 60 | NotExist("stuid") 61 | } 62 | 63 | func TestJSON_String(t *testing.T) { 64 | NewJSON([]byte(JSONAssertData), t). 65 | String("time", AssertDataTimeStr) 66 | } 67 | 68 | func TestJSON_Int(t *testing.T) { 69 | NewJSON([]byte(JSONAssertData), t). 70 | Int("number", int64(1)) 71 | } 72 | 73 | func TestJSON_True(t *testing.T) { 74 | NewJSON([]byte(JSONAssertData), t). 75 | True("ok") 76 | } 77 | 78 | func TestJSON_False(t *testing.T) { 79 | NewJSON([]byte(JSONAssertData), t). 80 | False("no") 81 | } 82 | 83 | func TestJSON_Uint(t *testing.T) { 84 | NewJSON([]byte(JSONAssertData), t). 85 | Uint("number", uint64(1)) 86 | } 87 | 88 | func TestJSON_Time(t *testing.T) { 89 | NewJSON([]byte(JSONAssertData), t). 90 | Time("time", AssertDataTime) 91 | } 92 | 93 | func TestJSON_Float(t *testing.T) { 94 | NewJSON([]byte(JSONAssertData), t). 95 | Float("number", float64(1)) 96 | } 97 | 98 | func TestJSON_Bind(t *testing.T) { 99 | user := new(User) 100 | NewClient(t). 101 | To(Mux). 102 | Get("/body/user"). 103 | Test(). 104 | StatusOK(). 105 | JSON(). 106 | Bind(user) 107 | assert.Equal(t, user.Id, uint(1)) 108 | assert.Equal(t, user.Name, "hexi") 109 | } 110 | 111 | func TestJSON_NotEmpty(t *testing.T) { 112 | NewXML([]byte(UserDataXML), t). 113 | JSON.NotEmpty() 114 | } 115 | 116 | func TestXML_Exist(t *testing.T) { 117 | NewClient(t). 118 | To(Mux). 119 | Get("/xml_body/user"). 120 | Test(). 121 | StatusOK(). 122 | XML(). 123 | Exist("user.name"). 124 | NotExist("user.stuid") 125 | } 126 | 127 | func TestXML_String(t *testing.T) { 128 | NewXML([]byte(XMLAssertData), t). 129 | String("data.Time", AssertDataTimeStr) 130 | } 131 | 132 | func TestXML_Int(t *testing.T) { 133 | NewXML([]byte(XMLAssertData), t). 134 | Int("data.Number", int64(1)) 135 | } 136 | 137 | func TestXML_True(t *testing.T) { 138 | NewXML([]byte(XMLAssertData), t). 139 | True("data.OK") 140 | } 141 | 142 | func TestXML_False(t *testing.T) { 143 | NewXML([]byte(XMLAssertData), t). 144 | False("data.NO") 145 | } 146 | 147 | func TestXML_Uint(t *testing.T) { 148 | NewXML([]byte(XMLAssertData), t). 149 | Uint("data.Number", uint64(1)) 150 | } 151 | 152 | func TestXML_Time(t *testing.T) { 153 | NewXML([]byte(XMLAssertData), t). 154 | Time("data.Time", AssertDataTime) 155 | } 156 | 157 | func TestXML_Float(t *testing.T) { 158 | NewXML([]byte(XMLAssertData), t). 159 | Float("data.Number", float64(1)) 160 | } 161 | 162 | func TestXML_Empty(t *testing.T) { 163 | NewXML([]byte(""), t). 164 | Empty() 165 | } 166 | 167 | func TestXML_NotEmpty(t *testing.T) { 168 | NewXML([]byte(WrongXMLData), t). 169 | NotEmpty() 170 | } 171 | 172 | // TO assure methods of XML never return JSON 173 | func TestXML_Bind_After_Assert(t *testing.T) { 174 | data := new(AssertStruct) 175 | NewXML([]byte(XMLAssertData), t). 176 | Exist("data.Time"). 177 | String("data.Time", AssertDataTimeStr). 178 | Int("data.Number", int64(1)). 179 | True("data.OK"). 180 | False("data.NO"). 181 | Uint("data.Number", uint64(1)). 182 | Time("data.Time", AssertDataTime). 183 | Float("data.Number", float64(1)). 184 | NotEmpty(). 185 | Bind(data) 186 | assert.Equal(t, 1, data.Number) 187 | assert.Equal(t, AssertDataTime, data.Time) 188 | assert.True(t, data.OK) 189 | assert.False(t, data.NO) 190 | } 191 | 192 | func TestWrongXML_JSON_Empty(t *testing.T) { 193 | NewXML([]byte(WrongXMLData), t). 194 | JSON.Empty() 195 | } 196 | 197 | func TestXML_Bind(t *testing.T) { 198 | user := new(User) 199 | NewClient(t).To(Mux). 200 | Get("/xml_body/user"). 201 | Test(). 202 | StatusOK(). 203 | XML(). 204 | Bind(user) 205 | assert.Equal(t, user.Id, uint(1)) 206 | assert.Equal(t, user.Name, "hexi") 207 | } 208 | 209 | func TestMD5_Expect(t *testing.T) { 210 | NewClient(t). 211 | To(Mux). 212 | Get("/body/user"). 213 | Test(). 214 | StatusOK(). 215 | MD5(). 216 | Expect(UserDataMD5) 217 | } 218 | 219 | func TestSHA1_Expect(t *testing.T) { 220 | NewClient(t). 221 | To(Mux). 222 | Get("/body/user"). 223 | Test(). 224 | StatusOK(). 225 | SHA1(). 226 | Expect(UserDataSHA1) 227 | } 228 | 229 | func UserDataHandler(w http.ResponseWriter, req *http.Request) { 230 | io.WriteString(w, UserData) 231 | } 232 | 233 | func UserDataXMLHandler(w http.ResponseWriter, req *http.Request) { 234 | io.WriteString(w, UserDataXML) 235 | } 236 | -------------------------------------------------------------------------------- /client.go: -------------------------------------------------------------------------------- 1 | package htest 2 | 3 | import ( 4 | "github.com/stretchr/testify/assert" 5 | "io" 6 | "net/http" 7 | "testing" 8 | ) 9 | 10 | type ( 11 | Client struct { 12 | handler http.Handler 13 | *testing.T 14 | } 15 | ) 16 | 17 | func NewClient(t *testing.T) *Client { 18 | return &Client{T: t} 19 | } 20 | 21 | func (c Client) To(handler http.Handler) *Client { 22 | c.handler = handler 23 | return &c 24 | } 25 | 26 | func (c Client) ToFunc(handlerFunc http.HandlerFunc) *Client { 27 | c.handler = handlerFunc 28 | return &c 29 | } 30 | 31 | func (c Client) NewRequest(req *http.Request) *Request { 32 | return &Request{ 33 | Request: req, 34 | Handler: c.handler, 35 | T: c.T, 36 | } 37 | } 38 | 39 | func (c Client) request(method, path string, body io.Reader) *Request { 40 | req, err := http.NewRequest(method, path, body) 41 | assert.Nil(c.T, err) 42 | return c.NewRequest(req) 43 | } 44 | 45 | func (c Client) Get(path string) *Request { 46 | return c.request(GET, path, nil) 47 | } 48 | 49 | func (c Client) Head(path string) *Request { 50 | return c.request(HEAD, path, nil) 51 | } 52 | 53 | func (c Client) Trace(path string) *Request { 54 | return c.request(TRACE, path, nil) 55 | } 56 | 57 | func (c Client) Options(path string) *Request { 58 | return c.request(OPTIONS, path, nil) 59 | } 60 | 61 | func (c Client) Connect(path string) *Request { 62 | return c.request(CONNECT, path, nil) 63 | } 64 | 65 | func (c Client) Delete(path string) *Request { 66 | return c.request(DELETE, path, nil) 67 | } 68 | 69 | func (c Client) Post(path string, body io.Reader) *Request { 70 | return c.request(POST, path, body) 71 | } 72 | 73 | func (c Client) Put(path string, body io.Reader) *Request { 74 | return c.request(PUT, path, body) 75 | } 76 | 77 | func (c Client) Patch(path string, body io.Reader) *Request { 78 | return c.request(PATCH, path, body) 79 | } 80 | -------------------------------------------------------------------------------- /client_test.go: -------------------------------------------------------------------------------- 1 | package htest 2 | 3 | import ( 4 | "bytes" 5 | "encoding/json" 6 | "github.com/stretchr/testify/assert" 7 | "io" 8 | "io/ioutil" 9 | "net/http" 10 | "testing" 11 | ) 12 | 13 | func TestClient_ToFunc(t *testing.T) { 14 | NewClient(t). 15 | ToFunc(NameHandler). 16 | Get(""). 17 | Test(). 18 | StatusOK().JSON().String("name", "hexi") 19 | } 20 | 21 | func TestClient_To(t *testing.T) { 22 | // if Client immutable 23 | client := NewClient(t) 24 | client.To(Mux) 25 | assert.Nil(t, client.handler) 26 | } 27 | 28 | func TestClient_Get(t *testing.T) { 29 | NewClient(t). 30 | To(Mux). 31 | Get("/client/get"). 32 | Test(). 33 | StatusOK().JSON().String("name", "hexi") 34 | } 35 | 36 | func TestClient_Trace(t *testing.T) { 37 | NewClient(t). 38 | To(Mux). 39 | Trace("/client/trace"). 40 | Test(). 41 | StatusOK().JSON().String("name", "hexi") 42 | } 43 | 44 | func TestClient_Connect(t *testing.T) { 45 | NewClient(t). 46 | To(Mux). 47 | Connect("/client/connect"). 48 | Test(). 49 | StatusOK().JSON().String("name", "hexi") 50 | } 51 | 52 | func TestClient_Delete(t *testing.T) { 53 | NewClient(t). 54 | To(Mux). 55 | Delete("/client/delete"). 56 | Test(). 57 | StatusOK().JSON().String("name", "hexi") 58 | } 59 | 60 | func TestClient_Options(t *testing.T) { 61 | NewClient(t). 62 | To(Mux). 63 | Options("/client/options"). 64 | Test(). 65 | StatusOK().JSON().String("name", "hexi") 66 | } 67 | 68 | func TestClient_Head(t *testing.T) { 69 | NewClient(t). 70 | To(Mux). 71 | Head("/client/head"). 72 | Test(). 73 | StatusOK() 74 | } 75 | 76 | func TestClient_Post(t *testing.T) { 77 | user := &User{Id: 0} 78 | data, _ := json.Marshal(user) 79 | dataReader := bytes.NewBuffer(data) 80 | NewClient(t). 81 | To(Mux). 82 | Post("/client/post", dataReader). 83 | Test(). 84 | StatusOK(). 85 | Bind(user) 86 | 87 | assert.Equal(t, uint(1), user.Id) 88 | assert.Equal(t, "hexi", user.Name) 89 | } 90 | 91 | func TestClient_Put(t *testing.T) { 92 | user := &User{Id: 0} 93 | data, _ := json.Marshal(user) 94 | dataReader := bytes.NewBuffer(data) 95 | NewClient(t). 96 | To(Mux). 97 | Put("/client/put", dataReader). 98 | Test(). 99 | StatusOK(). 100 | Bind(user) 101 | 102 | assert.Equal(t, uint(1), user.Id) 103 | assert.Equal(t, "hexi", user.Name) 104 | } 105 | 106 | func TestClient_Patch(t *testing.T) { 107 | user := &User{Id: 0} 108 | data, _ := json.Marshal(user) 109 | dataReader := bytes.NewBuffer(data) 110 | NewClient(t). 111 | To(Mux). 112 | Patch("/client/patch", dataReader). 113 | Test(). 114 | StatusOK(). 115 | Bind(user) 116 | 117 | assert.Equal(t, uint(1), user.Id) 118 | assert.Equal(t, "hexi", user.Name) 119 | } 120 | 121 | func ClientDataHandler(w http.ResponseWriter, req *http.Request) { 122 | user := new(User) 123 | resp, _ := ioutil.ReadAll(req.Body) 124 | json.Unmarshal(resp, user) 125 | if user.Id == 0 { 126 | io.WriteString(w, UserData) 127 | } 128 | } 129 | -------------------------------------------------------------------------------- /const.go: -------------------------------------------------------------------------------- 1 | package htest 2 | 3 | // HTTP methods 4 | const ( 5 | CONNECT = "CONNECT" 6 | DELETE = "DELETE" 7 | GET = "GET" 8 | HEAD = "HEAD" 9 | OPTIONS = "OPTIONS" 10 | PATCH = "PATCH" 11 | POST = "POST" 12 | PUT = "PUT" 13 | TRACE = "TRACE" 14 | ) 15 | 16 | // MIME types 17 | const ( 18 | MIMEApplicationJSON = "application/json" 19 | MIMEApplicationJSONCharsetUTF8 = MIMEApplicationJSON + "; " + charsetUTF8 20 | MIMEApplicationJavaScript = "application/javascript" 21 | MIMEApplicationJavaScriptCharsetUTF8 = MIMEApplicationJavaScript + "; " + charsetUTF8 22 | MIMEApplicationXML = "application/xml" 23 | MIMEApplicationXMLCharsetUTF8 = MIMEApplicationXML + "; " + charsetUTF8 24 | MIMETextXML = "text/xml" 25 | MIMETextXMLCharsetUTF8 = MIMETextXML + "; " + charsetUTF8 26 | MIMEApplicationForm = "application/x-www-form-urlencoded" 27 | MIMEApplicationProtobuf = "application/protobuf" 28 | MIMEApplicationMsgpack = "application/msgpack" 29 | MIMETextHTML = "text/html" 30 | MIMETextHTMLCharsetUTF8 = MIMETextHTML + "; " + charsetUTF8 31 | MIMETextPlain = "text/plain" 32 | MIMETextPlainCharsetUTF8 = MIMETextPlain + "; " + charsetUTF8 33 | MIMEMultipartForm = "multipart/form-data" 34 | MIMEOctetStream = "application/octet-stream" 35 | ) 36 | 37 | const ( 38 | charsetUTF8 = "charset=UTF-8" 39 | ) 40 | 41 | // Headers 42 | const ( 43 | HeaderAccept = "Accept" 44 | HeaderAcceptEncoding = "Accept-Encoding" 45 | HeaderAllow = "Allow" 46 | HeaderAuthorization = "Authorization" 47 | HeaderContentDisposition = "Content-Disposition" 48 | HeaderContentEncoding = "Content-Encoding" 49 | HeaderContentLength = "Content-Length" 50 | HeaderContentType = "Content-Type" 51 | HeaderCookie = "Cookie" 52 | HeaderSetCookie = "Set-Cookie" 53 | HeaderIfModifiedSince = "If-Modified-Since" 54 | HeaderLastModified = "Last-Modified" 55 | HeaderLocation = "Location" 56 | HeaderUpgrade = "Upgrade" 57 | HeaderVary = "Vary" 58 | HeaderWWWAuthenticate = "WWW-Authenticate" 59 | HeaderXForwardedFor = "X-Forwarded-For" 60 | HeaderXForwardedProto = "X-Forwarded-Proto" 61 | HeaderXForwardedProtocol = "X-Forwarded-Protocol" 62 | HeaderXForwardedSsl = "X-Forwarded-Ssl" 63 | HeaderXUrlScheme = "X-Url-Scheme" 64 | HeaderXHTTPMethodOverride = "X-HTTP-Method-Override" 65 | HeaderXRealIP = "X-Real-IP" 66 | HeaderXRequestID = "X-Request-ID" 67 | HeaderServer = "Server" 68 | HeaderOrigin = "Origin" 69 | 70 | // Access control 71 | HeaderAccessControlRequestMethod = "Access-Control-Request-Method" 72 | HeaderAccessControlRequestHeaders = "Access-Control-Request-Headers" 73 | HeaderAccessControlAllowOrigin = "Access-Control-Allow-Origin" 74 | HeaderAccessControlAllowMethods = "Access-Control-Allow-Methods" 75 | HeaderAccessControlAllowHeaders = "Access-Control-Allow-Headers" 76 | HeaderAccessControlAllowCredentials = "Access-Control-Allow-Credentials" 77 | HeaderAccessControlExposeHeaders = "Access-Control-Expose-Headers" 78 | HeaderAccessControlMaxAge = "Access-Control-Max-Age" 79 | 80 | // Security 81 | HeaderStrictTransportSecurity = "Strict-Transport-Security" 82 | HeaderXContentTypeOptions = "X-Content-Type-Options" 83 | HeaderXXSSProtection = "X-XSS-Protection" 84 | HeaderXFrameOptions = "X-Frame-Options" 85 | HeaderContentSecurityPolicy = "Content-Security-Policy" 86 | HeaderXCSRFToken = "X-CSRF-Token" 87 | ) 88 | -------------------------------------------------------------------------------- /doc.go: -------------------------------------------------------------------------------- 1 | /* 2 | htest is a http test package 3 | 4 | import "github.com/Hexilee/htest" 5 | */ 6 | package htest 7 | -------------------------------------------------------------------------------- /example/basic_mock_client.go: -------------------------------------------------------------------------------- 1 | package example 2 | 3 | import ( 4 | "github.com/labstack/echo" 5 | "io" 6 | "net/http" 7 | ) 8 | 9 | var ( 10 | Mux *http.ServeMux 11 | server *echo.Echo 12 | ) 13 | 14 | func init() { 15 | Mux = http.NewServeMux() 16 | Mux.HandleFunc("/name", NameHandler) 17 | server = echo.New() 18 | server.GET("/name", NameHandlerEcho) 19 | } 20 | 21 | func NameHandler(w http.ResponseWriter, req *http.Request) { 22 | io.WriteString(w, `{"name": "hexi"}`) 23 | } 24 | 25 | func NameHandlerEcho(c echo.Context) error { 26 | return c.String(http.StatusOK, `{"name": "hexi"}`) 27 | } 28 | -------------------------------------------------------------------------------- /example/basic_mock_client_test.go: -------------------------------------------------------------------------------- 1 | package example 2 | 3 | import ( 4 | "github.com/Hexilee/htest" 5 | "testing" 6 | ) 7 | 8 | func TestNameHandlerFunc(t *testing.T) { 9 | htest.NewClient(t). 10 | ToFunc(NameHandler). 11 | Get(""). 12 | Test(). 13 | StatusOK(). 14 | JSON(). 15 | String("name", "hexi") 16 | } 17 | 18 | func TestNameHandler(t *testing.T) { 19 | htest.NewClient(t). 20 | To(Mux). 21 | Get("/name"). 22 | Test(). 23 | StatusOK(). 24 | JSON(). 25 | String("name", "hexi") 26 | } 27 | 28 | func TestNameHandlerEcho(t *testing.T) { 29 | htest.NewClient(t). 30 | To(server). 31 | Get("/name"). 32 | Test(). 33 | StatusOK(). 34 | JSON(). 35 | String("name", "hexi") 36 | } 37 | -------------------------------------------------------------------------------- /example/glide.lock: -------------------------------------------------------------------------------- 1 | hash: e8cc3d08ee2b5091efc328931a9fe869010be26ce96f7f0c4587117feba59877 2 | updated: 2018-02-24T00:16:26.361545+08:00 3 | imports: 4 | - name: github.com/labstack/echo 5 | version: b338075a0fc6e1a0683dbf03d09b4957a289e26f 6 | - name: github.com/labstack/gommon 7 | version: 57409ada9da0f2afad6664c49502f8c50fbd8476 8 | subpackages: 9 | - color 10 | - log 11 | - name: github.com/mattn/go-colorable 12 | version: 6cc8b475d4682021d75d2cbe2bc481bec4ce98e5 13 | - name: github.com/mattn/go-isatty 14 | version: 6ca4dbf54d38eea1a992b3c722a76a5d1c4cb25c 15 | - name: github.com/valyala/bytebufferpool 16 | version: e746df99fe4a3986f4d4f79e13c1e0117ce9c2f7 17 | - name: github.com/valyala/fasttemplate 18 | version: dcecefd839c4193db0d35b88ec65b4c12d360ab0 19 | - name: golang.org/x/crypto 20 | version: 1875d0a70c90e57f11972aefd42276df65e895b9 21 | subpackages: 22 | - acme 23 | - acme/autocert 24 | - name: golang.org/x/sys 25 | version: 3dbebcf8efb6a5011a60c2b4591c1022a759af8a 26 | subpackages: 27 | - unix 28 | testImports: 29 | - name: github.com/davecgh/go-spew 30 | version: 8991bc29aa16c548c550c7ff78260e27b9ab7c73 31 | subpackages: 32 | - spew 33 | - name: github.com/Hexilee/htest 34 | version: 160265b4f8fb47293a2a07d1933b4fa621f79c1a 35 | - name: github.com/pmezard/go-difflib 36 | version: 792786c7400a136282c1664665ae0a8db921c6c2 37 | subpackages: 38 | - difflib 39 | - name: github.com/stretchr/testify 40 | version: 12b6f73e6084dad08a7c6e575284b177ecafbc71 41 | subpackages: 42 | - assert 43 | - name: github.com/tidwall/gjson 44 | version: 01f00f129617a6fe98941fb920d6c760241b54d2 45 | - name: github.com/tidwall/match 46 | version: 1731857f09b1f38450e2c12409748407822dc6be 47 | -------------------------------------------------------------------------------- /example/glide.yaml: -------------------------------------------------------------------------------- 1 | package: github.com/Hexilee/htest/example 2 | import: 3 | - package: github.com/labstack/echo 4 | version: ^3.2.6 5 | - package: github.com/labstack/gommon 6 | version: ^0.2.3 7 | subpackages: 8 | - color 9 | - log 10 | - package: golang.org/x/crypto 11 | subpackages: 12 | - acme/autocert 13 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/Hexilee/htest 2 | 3 | require ( 4 | github.com/basgys/goxml2json v1.1.0 5 | github.com/davecgh/go-spew v1.1.1 6 | github.com/go-chi/chi v3.3.2+incompatible 7 | github.com/labstack/echo v0.0.0-20171223171103-b338075a0fc6 8 | github.com/labstack/gommon v0.0.0-20170925052817-57409ada9da0 9 | github.com/mattn/go-colorable v0.0.0-20180115155639-6cc8b475d468 10 | github.com/mattn/go-isatty v0.0.4 11 | github.com/pmezard/go-difflib v1.0.0 12 | github.com/stretchr/testify v1.2.1 13 | github.com/tidwall/gjson v1.1.0 14 | github.com/tidwall/match v0.0.0-20171002075945-1731857f09b1 15 | github.com/valyala/bytebufferpool v1.0.0 16 | github.com/valyala/fasttemplate v0.0.0-20170224212429-dcecefd839c4 17 | golang.org/x/crypto v0.0.0-20180127211104-1875d0a70c90 18 | golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01 19 | golang.org/x/sys v0.0.0-20180130064325-3dbebcf8efb6 20 | golang.org/x/text v0.0.0-20180221121826-9e2b64d659da 21 | ) 22 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/basgys/goxml2json v1.1.0/go.mod h1:wH7a5Np/Q4QoECFIU8zTQlZwZkrilY0itPfecMw41Dw= 2 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 3 | github.com/go-chi/chi v3.3.2+incompatible/go.mod h1:eB3wogJHnLi3x/kFX2A+IbTBlXxmMeXJVKy9tTv1XzQ= 4 | github.com/labstack/echo v0.0.0-20171223171103-b338075a0fc6/go.mod h1:0INS7j/VjnFxD4E2wkz67b8cVwCLbBmJyDaka6Cmk1s= 5 | github.com/labstack/gommon v0.0.0-20170925052817-57409ada9da0/go.mod h1:/tj9csK2iPSBvn+3NLM9e52usepMtrd5ilFYA+wQNJ4= 6 | github.com/mattn/go-colorable v0.0.0-20180115155639-6cc8b475d468/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= 7 | github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= 8 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 9 | github.com/stretchr/testify v1.2.1/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= 10 | github.com/tidwall/gjson v1.1.0/go.mod h1:c/nTNbUr0E0OrXEhq1pwa8iEgc2DOt4ZZqAt1HtCkPA= 11 | github.com/tidwall/match v0.0.0-20171002075945-1731857f09b1/go.mod h1:LujAq0jyVjBy028G1WhWfIzbpQfMO8bBZ6Tyb0+pL9E= 12 | github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= 13 | github.com/valyala/fasttemplate v0.0.0-20170224212429-dcecefd839c4/go.mod h1:50wTf68f99/Zt14pr046Tgt3Lp2vLyFZKzbFXTOabXw= 14 | golang.org/x/crypto v0.0.0-20180127211104-1875d0a70c90/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= 15 | golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= 16 | golang.org/x/sys v0.0.0-20180130064325-3dbebcf8efb6/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 17 | golang.org/x/text v0.0.0-20180221121826-9e2b64d659da/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= 18 | -------------------------------------------------------------------------------- /request.go: -------------------------------------------------------------------------------- 1 | package htest 2 | 3 | import ( 4 | "github.com/stretchr/testify/assert" 5 | "net/http" 6 | "net/http/httptest" 7 | "testing" 8 | ) 9 | 10 | type ( 11 | Request struct { 12 | *http.Request 13 | Handler http.Handler 14 | *testing.T 15 | } 16 | ) 17 | 18 | const ( 19 | MockNilError = "Request.Handler cannot be nil, had you called Client.To or Client.ToFunc?" 20 | ) 21 | 22 | func (r *Request) SetHeader(key, value string) *Request { 23 | r.Header.Set(key, value) 24 | return r 25 | } 26 | 27 | func (r *Request) SetHeaders(headers map[string]string) *Request { 28 | var key, value string 29 | for key, value = range headers { 30 | r.Header.Set(key, value) 31 | } 32 | return r 33 | } 34 | 35 | func (r *Request) Test() *Response { 36 | if r.Handler == nil { 37 | panic(MockNilError) 38 | } 39 | recorder := httptest.NewRecorder() 40 | r.Handler.ServeHTTP(recorder, r.Request) 41 | return NewResponse(recorder.Result(), r.T) 42 | } 43 | 44 | func (r *Request) Send() *Response { 45 | resp, err := (&http.Client{}).Do(r.Request) 46 | assert.Nil(r.T, err) 47 | return NewResponse(resp, r.T) 48 | } 49 | 50 | func (r *Request) AddCookie(cookie *http.Cookie) *Request { 51 | r.Request.AddCookie(cookie) 52 | return r 53 | } 54 | -------------------------------------------------------------------------------- /request_test.go: -------------------------------------------------------------------------------- 1 | package htest 2 | 3 | import ( 4 | "fmt" 5 | "github.com/stretchr/testify/assert" 6 | "io" 7 | "net/http" 8 | "testing" 9 | ) 10 | 11 | var ( 12 | testCookie = http.Cookie{Name: "test_cookie", Value: "cookie_value"} 13 | ) 14 | 15 | func TestRequest_SetHeader(t *testing.T) { 16 | client := NewClient(t).To(Mux) 17 | // bad content type 18 | client. 19 | Get("/request/header"). 20 | SetHeader(HeaderContentType, MIMEApplicationForm). 21 | Test(). 22 | StatusBadRequest() 23 | 24 | // right 25 | client. 26 | Get("/request/header"). 27 | SetHeader(HeaderContentType, MIMEApplicationJSON). 28 | Test(). 29 | StatusOK(). 30 | JSON(). 31 | String("result", "JSON") 32 | } 33 | 34 | func TestRequest_SetHeaders(t *testing.T) { 35 | client := NewClient(t).To(Mux) 36 | // bad content type 37 | client.Get("/request/header"). 38 | SetHeaders( 39 | map[string]string{ 40 | HeaderContentType: MIMEApplicationForm, 41 | }, 42 | ). 43 | Test(). 44 | StatusBadRequest() 45 | 46 | // right 47 | client.Get("/request/header"). 48 | SetHeaders( 49 | map[string]string{ 50 | HeaderContentType: MIMEApplicationJSON, 51 | }, 52 | ). 53 | Test(). 54 | StatusOK(). 55 | JSON(). 56 | String("result", "JSON") 57 | } 58 | 59 | func TestRequest_Test(t *testing.T) { 60 | defer func() { 61 | assert.Equal(t, MockNilError, recover()) 62 | }() 63 | 64 | NewClient(t). 65 | Get("/request/header"). 66 | SetHeader(HeaderContentType, MIMEApplicationForm). 67 | Test(). 68 | StatusBadRequest() 69 | } 70 | 71 | func TestRequest_Send(t *testing.T) { 72 | NewClient(t). 73 | Get("https://api.github.com/users/Hexilee"). 74 | Send(). 75 | StatusOK(). 76 | JSON(). 77 | String("login", "Hexilee") 78 | } 79 | 80 | func TestRequest_AddCookie(t *testing.T) { 81 | client := NewClient(t). 82 | To(Mux) 83 | client. 84 | Get("/request/cookie"). 85 | Test(). 86 | StatusForbidden() 87 | client. 88 | Get("/request/cookie"). 89 | AddCookie(&testCookie). 90 | Test(). 91 | StatusOK(). 92 | JSON(). 93 | String("cookie", testCookie.String()) 94 | } 95 | 96 | func HeaderHandler(w http.ResponseWriter, req *http.Request) { 97 | if req.Header.Get(HeaderContentType) == MIMEApplicationJSON { 98 | io.WriteString(w, `{"result": "JSON"}`) 99 | return 100 | } 101 | http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) 102 | } 103 | 104 | func CookieHandler(w http.ResponseWriter, req *http.Request) { 105 | cookie, err := req.Cookie(testCookie.Name) 106 | if err != nil { 107 | http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden) 108 | return 109 | } 110 | io.WriteString(w, fmt.Sprintf(`{"cookie": "%s"}`, cookie)) 111 | } 112 | -------------------------------------------------------------------------------- /response.go: -------------------------------------------------------------------------------- 1 | package htest 2 | 3 | import ( 4 | "crypto/md5" 5 | "crypto/sha1" 6 | "encoding/json" 7 | "github.com/stretchr/testify/assert" 8 | "io" 9 | "io/ioutil" 10 | "net/http" 11 | "testing" 12 | ) 13 | 14 | type ( 15 | Response struct { 16 | *http.Response 17 | *testing.T 18 | } 19 | ) 20 | 21 | func NewResponse(response *http.Response, t *testing.T) *Response { 22 | return &Response{ 23 | Response: response, 24 | T: t, 25 | } 26 | } 27 | 28 | func (r *Response) Code(statusCode int) *Response { 29 | assert.Equal(r.T, statusCode, r.StatusCode) 30 | return r 31 | } 32 | 33 | // http.Response.Status of go 1.9+ is different from former version, so I comment this assert 34 | /* 35 | func (r *Response) Status(expect string) *Response { 36 | assert.Equal(r.T, expect, r.Response.Status) 37 | return r 38 | } 39 | */ 40 | 41 | func (r *Response) StatusContinue() *Response { 42 | return r.Code(http.StatusContinue) 43 | } 44 | func (r *Response) StatusSwitchingProtocols() *Response { 45 | return r.Code(http.StatusSwitchingProtocols) 46 | } 47 | func (r *Response) StatusProcessing() *Response { 48 | return r.Code(http.StatusProcessing) 49 | } 50 | func (r *Response) StatusOK() *Response { 51 | return r.Code(http.StatusOK) 52 | } 53 | func (r *Response) StatusCreated() *Response { 54 | return r.Code(http.StatusCreated) 55 | } 56 | func (r *Response) StatusAccepted() *Response { 57 | return r.Code(http.StatusAccepted) 58 | } 59 | func (r *Response) StatusNonAuthoritativeInfo() *Response { 60 | return r.Code(http.StatusNonAuthoritativeInfo) 61 | } 62 | func (r *Response) StatusNoContent() *Response { 63 | return r.Code(http.StatusNoContent) 64 | } 65 | func (r *Response) StatusResetContent() *Response { 66 | return r.Code(http.StatusResetContent) 67 | } 68 | func (r *Response) StatusPartialContent() *Response { 69 | return r.Code(http.StatusPartialContent) 70 | } 71 | func (r *Response) StatusMultiStatus() *Response { 72 | return r.Code(http.StatusMultiStatus) 73 | } 74 | func (r *Response) StatusAlreadyReported() *Response { 75 | return r.Code(http.StatusAlreadyReported) 76 | } 77 | func (r *Response) StatusIMUsed() *Response { 78 | return r.Code(http.StatusIMUsed) 79 | } 80 | func (r *Response) StatusMultipleChoices() *Response { 81 | return r.Code(http.StatusMultipleChoices) 82 | } 83 | func (r *Response) StatusMovedPermanently() *Response { 84 | return r.Code(http.StatusMovedPermanently) 85 | } 86 | func (r *Response) StatusFound() *Response { 87 | return r.Code(http.StatusFound) 88 | } 89 | func (r *Response) StatusSeeOther() *Response { 90 | return r.Code(http.StatusSeeOther) 91 | } 92 | func (r *Response) StatusNotModified() *Response { 93 | return r.Code(http.StatusNotModified) 94 | } 95 | func (r *Response) StatusUseProxy() *Response { 96 | return r.Code(http.StatusUseProxy) 97 | } 98 | func (r *Response) StatusTemporaryRedirect() *Response { 99 | return r.Code(http.StatusTemporaryRedirect) 100 | } 101 | func (r *Response) StatusPermanentRedirect() *Response { 102 | return r.Code(http.StatusPermanentRedirect) 103 | } 104 | func (r *Response) StatusBadRequest() *Response { 105 | return r.Code(http.StatusBadRequest) 106 | } 107 | func (r *Response) StatusUnauthorized() *Response { 108 | return r.Code(http.StatusUnauthorized) 109 | } 110 | func (r *Response) StatusPaymentRequired() *Response { 111 | return r.Code(http.StatusPaymentRequired) 112 | } 113 | func (r *Response) StatusForbidden() *Response { 114 | return r.Code(http.StatusForbidden) 115 | } 116 | func (r *Response) StatusNotFound() *Response { 117 | return r.Code(http.StatusNotFound) 118 | } 119 | func (r *Response) StatusMethodNotAllowed() *Response { 120 | return r.Code(http.StatusMethodNotAllowed) 121 | } 122 | func (r *Response) StatusNotAcceptable() *Response { 123 | return r.Code(http.StatusNotAcceptable) 124 | } 125 | func (r *Response) StatusProxyAuthRequired() *Response { 126 | return r.Code(http.StatusProxyAuthRequired) 127 | } 128 | func (r *Response) StatusRequestTimeout() *Response { 129 | return r.Code(http.StatusRequestTimeout) 130 | } 131 | func (r *Response) StatusConflict() *Response { 132 | return r.Code(http.StatusConflict) 133 | } 134 | func (r *Response) StatusGone() *Response { 135 | return r.Code(http.StatusGone) 136 | } 137 | func (r *Response) StatusLengthRequired() *Response { 138 | return r.Code(http.StatusLengthRequired) 139 | } 140 | func (r *Response) StatusPreconditionFailed() *Response { 141 | return r.Code(http.StatusPreconditionFailed) 142 | } 143 | func (r *Response) StatusRequestEntityTooLarge() *Response { 144 | return r.Code(http.StatusRequestEntityTooLarge) 145 | } 146 | func (r *Response) StatusRequestURITooLong() *Response { 147 | return r.Code(http.StatusRequestURITooLong) 148 | } 149 | func (r *Response) StatusUnsupportedMediaType() *Response { 150 | return r.Code(http.StatusUnsupportedMediaType) 151 | } 152 | func (r *Response) StatusRequestedRangeNotSatisfiable() *Response { 153 | return r.Code(http.StatusRequestedRangeNotSatisfiable) 154 | } 155 | func (r *Response) StatusExpectationFailed() *Response { 156 | return r.Code(http.StatusExpectationFailed) 157 | } 158 | func (r *Response) StatusTeapot() *Response { 159 | return r.Code(http.StatusTeapot) 160 | } 161 | func (r *Response) StatusUnprocessableEntity() *Response { 162 | return r.Code(http.StatusUnprocessableEntity) 163 | } 164 | func (r *Response) StatusLocked() *Response { 165 | return r.Code(http.StatusLocked) 166 | } 167 | func (r *Response) StatusFailedDependency() *Response { 168 | return r.Code(http.StatusFailedDependency) 169 | } 170 | func (r *Response) StatusUpgradeRequired() *Response { 171 | return r.Code(http.StatusUpgradeRequired) 172 | } 173 | func (r *Response) StatusPreconditionRequired() *Response { 174 | return r.Code(http.StatusPreconditionRequired) 175 | } 176 | func (r *Response) StatusTooManyRequests() *Response { 177 | return r.Code(http.StatusTooManyRequests) 178 | } 179 | func (r *Response) StatusRequestHeaderFieldsTooLarge() *Response { 180 | return r.Code(http.StatusRequestHeaderFieldsTooLarge) 181 | } 182 | func (r *Response) StatusUnavailableForLegalReasons() *Response { 183 | return r.Code(http.StatusUnavailableForLegalReasons) 184 | } 185 | func (r *Response) StatusInternalServerError() *Response { 186 | return r.Code(http.StatusInternalServerError) 187 | } 188 | func (r *Response) StatusNotImplemented() *Response { 189 | return r.Code(http.StatusNotImplemented) 190 | } 191 | func (r *Response) StatusBadGateway() *Response { 192 | return r.Code(http.StatusBadGateway) 193 | } 194 | func (r *Response) StatusServiceUnavailable() *Response { 195 | return r.Code(http.StatusServiceUnavailable) 196 | } 197 | func (r *Response) StatusGatewayTimeout() *Response { 198 | return r.Code(http.StatusGatewayTimeout) 199 | } 200 | func (r *Response) StatusHTTPVersionNotSupported() *Response { 201 | return r.Code(http.StatusHTTPVersionNotSupported) 202 | } 203 | func (r *Response) StatusVariantAlsoNegotiates() *Response { 204 | return r.Code(http.StatusVariantAlsoNegotiates) 205 | } 206 | func (r *Response) StatusInsufficientStorage() *Response { 207 | return r.Code(http.StatusInsufficientStorage) 208 | } 209 | func (r *Response) StatusLoopDetected() *Response { 210 | return r.Code(http.StatusLoopDetected) 211 | } 212 | func (r *Response) StatusNotExtended() *Response { 213 | return r.Code(http.StatusNotExtended) 214 | } 215 | func (r *Response) StatusNetworkAuthenticationRequired() *Response { 216 | return r.Code(http.StatusNetworkAuthenticationRequired) 217 | } 218 | 219 | func (r *Response) JSON() *JSON { 220 | body, err := ioutil.ReadAll(r.Response.Body) 221 | r.Response.Body.Close() 222 | assert.Nil(r.T, err) 223 | return NewJSON(body, r.T) 224 | } 225 | 226 | func (r *Response) XML() *XML { 227 | body, err := ioutil.ReadAll(r.Response.Body) 228 | r.Response.Body.Close() 229 | assert.Nil(r.T, err) 230 | return NewXML(body, r.T) 231 | } 232 | 233 | func (r *Response) Bytes() []byte { 234 | body, err := ioutil.ReadAll(r.Response.Body) 235 | r.Response.Body.Close() 236 | assert.Nil(r.T, err) 237 | return body 238 | } 239 | 240 | func (r *Response) String() string { 241 | body, err := ioutil.ReadAll(r.Response.Body) 242 | r.Response.Body.Close() 243 | assert.Nil(r.T, err) 244 | return string(body) 245 | } 246 | 247 | func (r *Response) Expect(expect string) { 248 | assert.Equal(r.T, expect, r.String()) 249 | } 250 | 251 | func (r *Response) MD5() *MD5 { 252 | buf := md5.New() 253 | io.Copy(buf, r.Response.Body) 254 | r.Response.Body.Close() 255 | result := buf.Sum(nil) 256 | return NewMD5(result, r.T) 257 | } 258 | 259 | func (r *Response) SHA1() *SHA1 { 260 | buf := sha1.New() 261 | io.Copy(buf, r.Response.Body) 262 | r.Response.Body.Close() 263 | result := buf.Sum(nil) 264 | return NewSHA1(result, r.T) 265 | } 266 | 267 | func (r *Response) Bind(obj interface{}) error { 268 | body, err := ioutil.ReadAll(r.Response.Body) 269 | r.Response.Body.Close() 270 | assert.Nil(r.T, err) 271 | return json.Unmarshal(body, obj) 272 | } 273 | 274 | func (r *Response) Headers(key, expect string) *Response { 275 | assert.Equal(r.T, expect, r.Header.Get(key)) 276 | return r 277 | } 278 | 279 | func (r *Response) HeaderAccept(expect string) *Response { 280 | return r.Headers(HeaderAccept, expect) 281 | } 282 | 283 | func (r *Response) HeaderAcceptEncoding(expect string) *Response { 284 | return r.Headers(HeaderAcceptEncoding, expect) 285 | } 286 | 287 | func (r *Response) HeaderAllow(expect string) *Response { 288 | return r.Headers(HeaderAllow, expect) 289 | } 290 | 291 | func (r *Response) HeaderAuthorization(expect string) *Response { 292 | return r.Headers(HeaderAuthorization, expect) 293 | } 294 | 295 | func (r *Response) HeaderContentDisposition(expect string) *Response { 296 | return r.Headers(HeaderContentDisposition, expect) 297 | } 298 | 299 | func (r *Response) HeaderContentEncoding(expect string) *Response { 300 | return r.Headers(HeaderContentEncoding, expect) 301 | } 302 | 303 | func (r *Response) HeaderContentLength(expect string) *Response { 304 | return r.Headers(HeaderContentLength, expect) 305 | } 306 | 307 | func (r *Response) HeaderContentType(expect string) *Response { 308 | return r.Headers(HeaderContentType, expect) 309 | } 310 | 311 | func (r *Response) HeaderCookie(expect string) *Response { 312 | return r.Headers(HeaderCookie, expect) 313 | } 314 | 315 | func (r *Response) HeaderSetCookie(expect string) *Response { 316 | return r.Headers(HeaderSetCookie, expect) 317 | } 318 | 319 | func (r *Response) HeaderIfModifiedSince(expect string) *Response { 320 | return r.Headers(HeaderIfModifiedSince, expect) 321 | } 322 | 323 | func (r *Response) HeaderLastModified(expect string) *Response { 324 | return r.Headers(HeaderLastModified, expect) 325 | } 326 | 327 | func (r *Response) HeaderLocation(expect string) *Response { 328 | return r.Headers(HeaderLocation, expect) 329 | } 330 | 331 | func (r *Response) HeaderUpgrade(expect string) *Response { 332 | return r.Headers(HeaderUpgrade, expect) 333 | } 334 | 335 | func (r *Response) HeaderVary(expect string) *Response { 336 | return r.Headers(HeaderVary, expect) 337 | } 338 | 339 | func (r *Response) HeaderWWWAuthenticate(expect string) *Response { 340 | return r.Headers(HeaderWWWAuthenticate, expect) 341 | } 342 | 343 | func (r *Response) HeaderXForwardedFor(expect string) *Response { 344 | return r.Headers(HeaderXForwardedFor, expect) 345 | } 346 | 347 | func (r *Response) HeaderXForwardedProto(expect string) *Response { 348 | return r.Headers(HeaderXForwardedProto, expect) 349 | } 350 | 351 | func (r *Response) HeaderXForwardedProtocol(expect string) *Response { 352 | return r.Headers(HeaderXForwardedProtocol, expect) 353 | } 354 | 355 | func (r *Response) HeaderXForwardedSsl(expect string) *Response { 356 | return r.Headers(HeaderXForwardedSsl, expect) 357 | } 358 | 359 | func (r *Response) HeaderXUrlScheme(expect string) *Response { 360 | return r.Headers(HeaderXUrlScheme, expect) 361 | } 362 | 363 | func (r *Response) HeaderXHTTPMethodOverride(expect string) *Response { 364 | return r.Headers(HeaderXHTTPMethodOverride, expect) 365 | } 366 | 367 | func (r *Response) HeaderXRealIP(expect string) *Response { 368 | return r.Headers(HeaderXRealIP, expect) 369 | } 370 | 371 | func (r *Response) HeaderXRequestID(expect string) *Response { 372 | return r.Headers(HeaderXRequestID, expect) 373 | } 374 | 375 | func (r *Response) HeaderServer(expect string) *Response { 376 | return r.Headers(HeaderServer, expect) 377 | } 378 | 379 | func (r *Response) HeaderOrigin(expect string) *Response { 380 | return r.Headers(HeaderOrigin, expect) 381 | } 382 | 383 | func (r *Response) HeaderAccessControlRequestMethod(expect string) *Response { 384 | return r.Headers(HeaderAccessControlRequestMethod, expect) 385 | } 386 | 387 | func (r *Response) HeaderAccessControlRequestHeaders(expect string) *Response { 388 | return r.Headers(HeaderAccessControlRequestHeaders, expect) 389 | } 390 | 391 | func (r *Response) HeaderAccessControlAllowOrigin(expect string) *Response { 392 | return r.Headers(HeaderAccessControlAllowOrigin, expect) 393 | } 394 | 395 | func (r *Response) HeaderAccessControlAllowMethods(expect string) *Response { 396 | return r.Headers(HeaderAccessControlAllowMethods, expect) 397 | } 398 | 399 | func (r *Response) HeaderAccessControlAllowHeaders(expect string) *Response { 400 | return r.Headers(HeaderAccessControlAllowHeaders, expect) 401 | } 402 | 403 | func (r *Response) HeaderAccessControlAllowCredentials(expect string) *Response { 404 | return r.Headers(HeaderAccessControlAllowCredentials, expect) 405 | } 406 | 407 | func (r *Response) HeaderAccessControlExposeHeaders(expect string) *Response { 408 | return r.Headers(HeaderAccessControlExposeHeaders, expect) 409 | } 410 | 411 | func (r *Response) HeaderAccessControlMaxAge(expect string) *Response { 412 | return r.Headers(HeaderAccessControlMaxAge, expect) 413 | } 414 | 415 | func (r *Response) HeaderStrictTransportSecurity(expect string) *Response { 416 | return r.Headers(HeaderStrictTransportSecurity, expect) 417 | } 418 | 419 | func (r *Response) HeaderXContentTypeOptions(expect string) *Response { 420 | return r.Headers(HeaderXContentTypeOptions, expect) 421 | } 422 | 423 | func (r *Response) HeaderXXSSProtection(expect string) *Response { 424 | return r.Headers(HeaderXXSSProtection, expect) 425 | } 426 | 427 | func (r *Response) HeaderXFrameOptions(expect string) *Response { 428 | return r.Headers(HeaderXFrameOptions, expect) 429 | } 430 | 431 | func (r *Response) HeaderContentSecurityPolicy(expect string) *Response { 432 | return r.Headers(HeaderContentSecurityPolicy, expect) 433 | } 434 | 435 | func (r *Response) HeaderXCSRFToken(expect string) *Response { 436 | return r.Headers(HeaderXCSRFToken, expect) 437 | } 438 | -------------------------------------------------------------------------------- /response_test.go: -------------------------------------------------------------------------------- 1 | package htest 2 | 3 | import ( 4 | "crypto/md5" 5 | "crypto/sha1" 6 | "fmt" 7 | "github.com/go-chi/chi" 8 | "github.com/stretchr/testify/assert" 9 | "net/http" 10 | "strconv" 11 | "testing" 12 | ) 13 | 14 | type ( 15 | User struct { 16 | Id uint `xml:"id"` 17 | Name string `xml:"name"` 18 | } 19 | ) 20 | 21 | const ( 22 | UserData = `{ 23 | "id": 1, 24 | "name": "hexi" 25 | }` 26 | UserDataXML = ` 27 | 28 | 29 | 1 30 | hexi 31 | 32 | ` 33 | ) 34 | 35 | var ( 36 | ResponseCodeServer = chi.NewRouter() 37 | ResponseHeadersServer = chi.NewRouter() 38 | UserDataMD5 string 39 | UserDataSHA1 string 40 | ) 41 | 42 | func init() { 43 | ResponseCodeServer.Get("/response/statusCode/{code}", StatusHandler) 44 | ResponseHeadersServer.Get("/response/headers", HeadersHandler) 45 | 46 | UserMD5 := md5.New() 47 | UserMD5.Write([]byte(UserData)) 48 | UserDataMD5 = string(UserMD5.Sum(nil)) 49 | 50 | UserSHA1 := sha1.New() 51 | UserSHA1.Write([]byte(UserData)) 52 | UserDataSHA1 = string(UserSHA1.Sum(nil)) 53 | 54 | } 55 | 56 | func TestResponse_String(t *testing.T) { 57 | assert.Equal(t, UserData, NewClient(t). 58 | To(Mux). 59 | Get("/body/user"). 60 | Test(). 61 | StatusOK(). 62 | String()) 63 | } 64 | 65 | func TestResponse_Bytes(t *testing.T) { 66 | assert.Equal(t, []byte(UserData), NewClient(t). 67 | To(Mux). 68 | Get("/body/user"). 69 | Test(). 70 | StatusOK(). 71 | Bytes()) 72 | } 73 | 74 | func TestResponse_Expect(t *testing.T) { 75 | NewClient(t). 76 | To(Mux). 77 | Get("/body/user"). 78 | Test(). 79 | StatusOK(). 80 | Expect(UserData) 81 | } 82 | 83 | func TestResponse_Bind(t *testing.T) { 84 | user := new(User) 85 | NewClient(t). 86 | To(Mux). 87 | Get("/body/user"). 88 | Test(). 89 | StatusOK(). 90 | Bind(user) 91 | assert.Equal(t, user.Id, uint(1)) 92 | assert.Equal(t, user.Name, "hexi") 93 | } 94 | 95 | func TestResponse_Code(t *testing.T) { 96 | NewClient(t). 97 | To(ResponseCodeServer). 98 | Get(fmt.Sprintf("/response/statusCode/%d", http.StatusBadRequest)). 99 | Test(). 100 | Code(http.StatusBadRequest) 101 | } 102 | 103 | func TestResponse_Headers(t *testing.T) { 104 | url := fmt.Sprintf("/response/headers?header=%s&value=%s", HeaderContentType, MIMEApplicationJSON) 105 | NewClient(t). 106 | To(ResponseHeadersServer). 107 | Get(url). 108 | Test(). 109 | Headers(HeaderContentType, MIMEApplicationJSON) 110 | } 111 | 112 | // http.Response.Status of go 1.9+ is different from former version, so I comment this assert 113 | /* 114 | func TestResponse_Status(t *testing.T) { 115 | NewClient(t). 116 | To(ResponseCodeServer). 117 | Get(fmt.Sprintf("/response/statusCode/%d", http.StatusContinue)). 118 | Test(). 119 | Status(fmt.Sprintf("%d %s", http.StatusContinue, http.StatusText(http.StatusContinue))) 120 | } 121 | */ 122 | 123 | func TestResponse_StatusContinue(t *testing.T) { 124 | NewClient(t). 125 | To(ResponseCodeServer). 126 | Get(fmt.Sprintf("/response/statusCode/%d", http.StatusContinue)). 127 | Test(). 128 | StatusContinue() 129 | } 130 | 131 | func TestResponse_StatusSwitchingProtocols(t *testing.T) { 132 | NewClient(t). 133 | To(ResponseCodeServer). 134 | Get(fmt.Sprintf("/response/statusCode/%d", http.StatusSwitchingProtocols)). 135 | Test(). 136 | StatusSwitchingProtocols() 137 | } 138 | 139 | func TestResponse_StatusProcessing(t *testing.T) { 140 | NewClient(t). 141 | To(ResponseCodeServer). 142 | Get(fmt.Sprintf("/response/statusCode/%d", http.StatusProcessing)). 143 | Test(). 144 | StatusProcessing() 145 | } 146 | 147 | func TestResponse_StatusOK(t *testing.T) { 148 | NewClient(t). 149 | To(ResponseCodeServer). 150 | Get(fmt.Sprintf("/response/statusCode/%d", http.StatusOK)). 151 | Test(). 152 | StatusOK() 153 | } 154 | 155 | func TestResponse_StatusCreated(t *testing.T) { 156 | NewClient(t). 157 | To(ResponseCodeServer). 158 | Get(fmt.Sprintf("/response/statusCode/%d", http.StatusCreated)). 159 | Test(). 160 | StatusCreated() 161 | } 162 | 163 | func TestResponse_StatusAccepted(t *testing.T) { 164 | NewClient(t). 165 | To(ResponseCodeServer). 166 | Get(fmt.Sprintf("/response/statusCode/%d", http.StatusAccepted)). 167 | Test(). 168 | StatusAccepted() 169 | } 170 | 171 | func TestResponse_StatusNonAuthoritativeInfo(t *testing.T) { 172 | NewClient(t). 173 | To(ResponseCodeServer). 174 | Get(fmt.Sprintf("/response/statusCode/%d", http.StatusNonAuthoritativeInfo)). 175 | Test(). 176 | StatusNonAuthoritativeInfo() 177 | } 178 | 179 | func TestResponse_StatusNoContent(t *testing.T) { 180 | NewClient(t). 181 | To(ResponseCodeServer). 182 | Get(fmt.Sprintf("/response/statusCode/%d", http.StatusNoContent)). 183 | Test(). 184 | StatusNoContent() 185 | } 186 | 187 | func TestResponse_StatusResetContent(t *testing.T) { 188 | NewClient(t). 189 | To(ResponseCodeServer). 190 | Get(fmt.Sprintf("/response/statusCode/%d", http.StatusResetContent)). 191 | Test(). 192 | StatusResetContent() 193 | } 194 | 195 | func TestResponse_StatusPartialContent(t *testing.T) { 196 | NewClient(t). 197 | To(ResponseCodeServer). 198 | Get(fmt.Sprintf("/response/statusCode/%d", http.StatusPartialContent)). 199 | Test(). 200 | StatusPartialContent() 201 | } 202 | 203 | func TestResponse_StatusMultiStatus(t *testing.T) { 204 | NewClient(t). 205 | To(ResponseCodeServer). 206 | Get(fmt.Sprintf("/response/statusCode/%d", http.StatusMultiStatus)). 207 | Test(). 208 | StatusMultiStatus() 209 | } 210 | 211 | func TestResponse_StatusAlreadyReported(t *testing.T) { 212 | NewClient(t). 213 | To(ResponseCodeServer). 214 | Get(fmt.Sprintf("/response/statusCode/%d", http.StatusAlreadyReported)). 215 | Test(). 216 | StatusAlreadyReported() 217 | } 218 | 219 | func TestResponse_StatusIMUsed(t *testing.T) { 220 | NewClient(t). 221 | To(ResponseCodeServer). 222 | Get(fmt.Sprintf("/response/statusCode/%d", http.StatusIMUsed)). 223 | Test(). 224 | StatusIMUsed() 225 | } 226 | 227 | func TestResponse_StatusMultipleChoices(t *testing.T) { 228 | NewClient(t). 229 | To(ResponseCodeServer). 230 | Get(fmt.Sprintf("/response/statusCode/%d", http.StatusMultipleChoices)). 231 | Test(). 232 | StatusMultipleChoices() 233 | } 234 | 235 | func TestResponse_StatusMovedPermanently(t *testing.T) { 236 | NewClient(t). 237 | To(ResponseCodeServer). 238 | Get(fmt.Sprintf("/response/statusCode/%d", http.StatusMovedPermanently)). 239 | Test(). 240 | StatusMovedPermanently() 241 | } 242 | 243 | func TestResponse_StatusFound(t *testing.T) { 244 | NewClient(t). 245 | To(ResponseCodeServer). 246 | Get(fmt.Sprintf("/response/statusCode/%d", http.StatusFound)). 247 | Test(). 248 | StatusFound() 249 | } 250 | 251 | func TestResponse_StatusSeeOther(t *testing.T) { 252 | NewClient(t). 253 | To(ResponseCodeServer). 254 | Get(fmt.Sprintf("/response/statusCode/%d", http.StatusSeeOther)). 255 | Test(). 256 | StatusSeeOther() 257 | } 258 | 259 | func TestResponse_StatusNotModified(t *testing.T) { 260 | NewClient(t). 261 | To(ResponseCodeServer). 262 | Get(fmt.Sprintf("/response/statusCode/%d", http.StatusNotModified)). 263 | Test(). 264 | StatusNotModified() 265 | } 266 | 267 | func TestResponse_StatusUseProxy(t *testing.T) { 268 | NewClient(t). 269 | To(ResponseCodeServer). 270 | Get(fmt.Sprintf("/response/statusCode/%d", http.StatusUseProxy)). 271 | Test(). 272 | StatusUseProxy() 273 | } 274 | 275 | func TestResponse_StatusTemporaryRedirect(t *testing.T) { 276 | NewClient(t). 277 | To(ResponseCodeServer). 278 | Get(fmt.Sprintf("/response/statusCode/%d", http.StatusTemporaryRedirect)). 279 | Test(). 280 | StatusTemporaryRedirect() 281 | } 282 | 283 | func TestResponse_StatusPermanentRedirect(t *testing.T) { 284 | NewClient(t). 285 | To(ResponseCodeServer). 286 | Get(fmt.Sprintf("/response/statusCode/%d", http.StatusPermanentRedirect)). 287 | Test(). 288 | StatusPermanentRedirect() 289 | } 290 | 291 | func TestResponse_StatusBadRequest(t *testing.T) { 292 | NewClient(t). 293 | To(ResponseCodeServer). 294 | Get(fmt.Sprintf("/response/statusCode/%d", http.StatusBadRequest)). 295 | Test(). 296 | StatusBadRequest() 297 | } 298 | 299 | func TestResponse_StatusUnauthorized(t *testing.T) { 300 | NewClient(t). 301 | To(ResponseCodeServer). 302 | Get(fmt.Sprintf("/response/statusCode/%d", http.StatusUnauthorized)). 303 | Test(). 304 | StatusUnauthorized() 305 | } 306 | 307 | func TestResponse_StatusPaymentRequired(t *testing.T) { 308 | NewClient(t). 309 | To(ResponseCodeServer). 310 | Get(fmt.Sprintf("/response/statusCode/%d", http.StatusPaymentRequired)). 311 | Test(). 312 | StatusPaymentRequired() 313 | } 314 | 315 | func TestResponse_StatusForbidden(t *testing.T) { 316 | NewClient(t). 317 | To(ResponseCodeServer). 318 | Get(fmt.Sprintf("/response/statusCode/%d", http.StatusForbidden)). 319 | Test(). 320 | StatusForbidden() 321 | } 322 | 323 | func TestResponse_StatusNotFound(t *testing.T) { 324 | NewClient(t). 325 | To(ResponseCodeServer). 326 | Get(fmt.Sprintf("/response/statusCode/%d", http.StatusNotFound)). 327 | Test(). 328 | StatusNotFound() 329 | } 330 | 331 | func TestResponse_StatusMethodNotAllowed(t *testing.T) { 332 | NewClient(t). 333 | To(ResponseCodeServer). 334 | Get(fmt.Sprintf("/response/statusCode/%d", http.StatusMethodNotAllowed)). 335 | Test(). 336 | StatusMethodNotAllowed() 337 | } 338 | 339 | func TestResponse_StatusNotAcceptable(t *testing.T) { 340 | NewClient(t). 341 | To(ResponseCodeServer). 342 | Get(fmt.Sprintf("/response/statusCode/%d", http.StatusNotAcceptable)). 343 | Test(). 344 | StatusNotAcceptable() 345 | } 346 | 347 | func TestResponse_StatusProxyAuthRequired(t *testing.T) { 348 | NewClient(t). 349 | To(ResponseCodeServer). 350 | Get(fmt.Sprintf("/response/statusCode/%d", http.StatusProxyAuthRequired)). 351 | Test(). 352 | StatusProxyAuthRequired() 353 | } 354 | 355 | func TestResponse_StatusRequestTimeout(t *testing.T) { 356 | NewClient(t). 357 | To(ResponseCodeServer). 358 | Get(fmt.Sprintf("/response/statusCode/%d", http.StatusRequestTimeout)). 359 | Test(). 360 | StatusRequestTimeout() 361 | } 362 | 363 | func TestResponse_StatusConflict(t *testing.T) { 364 | NewClient(t). 365 | To(ResponseCodeServer). 366 | Get(fmt.Sprintf("/response/statusCode/%d", http.StatusConflict)). 367 | Test(). 368 | StatusConflict() 369 | } 370 | 371 | func TestResponse_StatusGone(t *testing.T) { 372 | NewClient(t). 373 | To(ResponseCodeServer). 374 | Get(fmt.Sprintf("/response/statusCode/%d", http.StatusGone)). 375 | Test(). 376 | StatusGone() 377 | } 378 | 379 | func TestResponse_StatusLengthRequired(t *testing.T) { 380 | NewClient(t). 381 | To(ResponseCodeServer). 382 | Get(fmt.Sprintf("/response/statusCode/%d", http.StatusLengthRequired)). 383 | Test(). 384 | StatusLengthRequired() 385 | } 386 | 387 | func TestResponse_StatusPreconditionFailed(t *testing.T) { 388 | NewClient(t). 389 | To(ResponseCodeServer). 390 | Get(fmt.Sprintf("/response/statusCode/%d", http.StatusPreconditionFailed)). 391 | Test(). 392 | StatusPreconditionFailed() 393 | } 394 | 395 | func TestResponse_StatusRequestEntityTooLarge(t *testing.T) { 396 | NewClient(t). 397 | To(ResponseCodeServer). 398 | Get(fmt.Sprintf("/response/statusCode/%d", http.StatusRequestEntityTooLarge)). 399 | Test(). 400 | StatusRequestEntityTooLarge() 401 | } 402 | 403 | func TestResponse_StatusRequestURITooLong(t *testing.T) { 404 | NewClient(t). 405 | To(ResponseCodeServer). 406 | Get(fmt.Sprintf("/response/statusCode/%d", http.StatusRequestURITooLong)). 407 | Test(). 408 | StatusRequestURITooLong() 409 | } 410 | 411 | func TestResponse_StatusUnsupportedMediaType(t *testing.T) { 412 | NewClient(t). 413 | To(ResponseCodeServer). 414 | Get(fmt.Sprintf("/response/statusCode/%d", http.StatusUnsupportedMediaType)). 415 | Test(). 416 | StatusUnsupportedMediaType() 417 | } 418 | 419 | func TestResponse_StatusRequestedRangeNotSatisfiable(t *testing.T) { 420 | NewClient(t). 421 | To(ResponseCodeServer). 422 | Get(fmt.Sprintf("/response/statusCode/%d", http.StatusRequestedRangeNotSatisfiable)). 423 | Test(). 424 | StatusRequestedRangeNotSatisfiable() 425 | } 426 | 427 | func TestResponse_StatusExpectationFailed(t *testing.T) { 428 | NewClient(t). 429 | To(ResponseCodeServer). 430 | Get(fmt.Sprintf("/response/statusCode/%d", http.StatusExpectationFailed)). 431 | Test(). 432 | StatusExpectationFailed() 433 | } 434 | 435 | func TestResponse_StatusTeapot(t *testing.T) { 436 | NewClient(t). 437 | To(ResponseCodeServer). 438 | Get(fmt.Sprintf("/response/statusCode/%d", http.StatusTeapot)). 439 | Test(). 440 | StatusTeapot() 441 | } 442 | 443 | func TestResponse_StatusUnprocessableEntity(t *testing.T) { 444 | NewClient(t). 445 | To(ResponseCodeServer). 446 | Get(fmt.Sprintf("/response/statusCode/%d", http.StatusUnprocessableEntity)). 447 | Test(). 448 | StatusUnprocessableEntity() 449 | } 450 | 451 | func TestResponse_StatusLocked(t *testing.T) { 452 | NewClient(t). 453 | To(ResponseCodeServer). 454 | Get(fmt.Sprintf("/response/statusCode/%d", http.StatusLocked)). 455 | Test(). 456 | StatusLocked() 457 | } 458 | 459 | func TestResponse_StatusFailedDependency(t *testing.T) { 460 | NewClient(t). 461 | To(ResponseCodeServer). 462 | Get(fmt.Sprintf("/response/statusCode/%d", http.StatusFailedDependency)). 463 | Test(). 464 | StatusFailedDependency() 465 | } 466 | 467 | func TestResponse_StatusUpgradeRequired(t *testing.T) { 468 | NewClient(t). 469 | To(ResponseCodeServer). 470 | Get(fmt.Sprintf("/response/statusCode/%d", http.StatusUpgradeRequired)). 471 | Test(). 472 | StatusUpgradeRequired() 473 | } 474 | 475 | func TestResponse_StatusPreconditionRequired(t *testing.T) { 476 | NewClient(t). 477 | To(ResponseCodeServer). 478 | Get(fmt.Sprintf("/response/statusCode/%d", http.StatusPreconditionRequired)). 479 | Test(). 480 | StatusPreconditionRequired() 481 | } 482 | 483 | func TestResponse_StatusTooManyRequests(t *testing.T) { 484 | NewClient(t). 485 | To(ResponseCodeServer). 486 | Get(fmt.Sprintf("/response/statusCode/%d", http.StatusTooManyRequests)). 487 | Test(). 488 | StatusTooManyRequests() 489 | } 490 | 491 | func TestResponse_StatusRequestHeaderFieldsTooLarge(t *testing.T) { 492 | NewClient(t). 493 | To(ResponseCodeServer). 494 | Get(fmt.Sprintf("/response/statusCode/%d", http.StatusRequestHeaderFieldsTooLarge)). 495 | Test(). 496 | StatusRequestHeaderFieldsTooLarge() 497 | } 498 | 499 | func TestResponse_StatusUnavailableForLegalReasons(t *testing.T) { 500 | NewClient(t). 501 | To(ResponseCodeServer). 502 | Get(fmt.Sprintf("/response/statusCode/%d", http.StatusUnavailableForLegalReasons)). 503 | Test(). 504 | StatusUnavailableForLegalReasons() 505 | } 506 | 507 | func TestResponse_StatusInternalServerError(t *testing.T) { 508 | NewClient(t). 509 | To(ResponseCodeServer). 510 | Get(fmt.Sprintf("/response/statusCode/%d", http.StatusInternalServerError)). 511 | Test(). 512 | StatusInternalServerError() 513 | } 514 | 515 | func TestResponse_StatusNotImplemented(t *testing.T) { 516 | NewClient(t). 517 | To(ResponseCodeServer). 518 | Get(fmt.Sprintf("/response/statusCode/%d", http.StatusNotImplemented)). 519 | Test(). 520 | StatusNotImplemented() 521 | } 522 | 523 | func TestResponse_StatusBadGateway(t *testing.T) { 524 | NewClient(t). 525 | To(ResponseCodeServer). 526 | Get(fmt.Sprintf("/response/statusCode/%d", http.StatusBadGateway)). 527 | Test(). 528 | StatusBadGateway() 529 | } 530 | 531 | func TestResponse_StatusServiceUnavailable(t *testing.T) { 532 | NewClient(t). 533 | To(ResponseCodeServer). 534 | Get(fmt.Sprintf("/response/statusCode/%d", http.StatusServiceUnavailable)). 535 | Test(). 536 | StatusServiceUnavailable() 537 | } 538 | 539 | func TestResponse_StatusGatewayTimeout(t *testing.T) { 540 | NewClient(t). 541 | To(ResponseCodeServer). 542 | Get(fmt.Sprintf("/response/statusCode/%d", http.StatusGatewayTimeout)). 543 | Test(). 544 | StatusGatewayTimeout() 545 | } 546 | 547 | func TestResponse_StatusHTTPVersionNotSupported(t *testing.T) { 548 | NewClient(t). 549 | To(ResponseCodeServer). 550 | Get(fmt.Sprintf("/response/statusCode/%d", http.StatusHTTPVersionNotSupported)). 551 | Test(). 552 | StatusHTTPVersionNotSupported() 553 | } 554 | 555 | func TestResponse_StatusVariantAlsoNegotiates(t *testing.T) { 556 | NewClient(t). 557 | To(ResponseCodeServer). 558 | Get(fmt.Sprintf("/response/statusCode/%d", http.StatusVariantAlsoNegotiates)). 559 | Test(). 560 | StatusVariantAlsoNegotiates() 561 | } 562 | 563 | func TestResponse_StatusInsufficientStorage(t *testing.T) { 564 | NewClient(t). 565 | To(ResponseCodeServer). 566 | Get(fmt.Sprintf("/response/statusCode/%d", http.StatusInsufficientStorage)). 567 | Test(). 568 | StatusInsufficientStorage() 569 | } 570 | 571 | func TestResponse_StatusLoopDetected(t *testing.T) { 572 | NewClient(t). 573 | To(ResponseCodeServer). 574 | Get(fmt.Sprintf("/response/statusCode/%d", http.StatusLoopDetected)). 575 | Test(). 576 | StatusLoopDetected() 577 | } 578 | 579 | func TestResponse_StatusNotExtended(t *testing.T) { 580 | NewClient(t). 581 | To(ResponseCodeServer). 582 | Get(fmt.Sprintf("/response/statusCode/%d", http.StatusNotExtended)). 583 | Test(). 584 | StatusNotExtended() 585 | } 586 | 587 | func TestResponse_StatusNetworkAuthenticationRequired(t *testing.T) { 588 | NewClient(t). 589 | To(ResponseCodeServer). 590 | Get(fmt.Sprintf("/response/statusCode/%d", http.StatusNetworkAuthenticationRequired)). 591 | Test(). 592 | StatusNetworkAuthenticationRequired() 593 | } 594 | 595 | func TestResponse_HeaderAccept(t *testing.T) { 596 | url := fmt.Sprintf("/response/headers?header=%s&value=%s", HeaderAccept, "htest") 597 | NewClient(t). 598 | To(ResponseHeadersServer). 599 | Get(url). 600 | Test(). 601 | HeaderAccept("htest") 602 | } 603 | 604 | func TestResponse_HeaderAcceptEncoding(t *testing.T) { 605 | url := fmt.Sprintf("/response/headers?header=%s&value=%s", HeaderAcceptEncoding, "htest") 606 | NewClient(t). 607 | To(ResponseHeadersServer). 608 | Get(url). 609 | Test(). 610 | HeaderAcceptEncoding("htest") 611 | } 612 | 613 | func TestResponse_HeaderAllow(t *testing.T) { 614 | url := fmt.Sprintf("/response/headers?header=%s&value=%s", HeaderAllow, "htest") 615 | NewClient(t). 616 | To(ResponseHeadersServer). 617 | Get(url). 618 | Test(). 619 | HeaderAllow("htest") 620 | } 621 | 622 | func TestResponse_HeaderAuthorization(t *testing.T) { 623 | url := fmt.Sprintf("/response/headers?header=%s&value=%s", HeaderAuthorization, "htest") 624 | NewClient(t). 625 | To(ResponseHeadersServer). 626 | Get(url). 627 | Test(). 628 | HeaderAuthorization("htest") 629 | } 630 | 631 | func TestResponse_HeaderContentDisposition(t *testing.T) { 632 | url := fmt.Sprintf("/response/headers?header=%s&value=%s", HeaderContentDisposition, "htest") 633 | NewClient(t). 634 | To(ResponseHeadersServer). 635 | Get(url). 636 | Test(). 637 | HeaderContentDisposition("htest") 638 | } 639 | 640 | func TestResponse_HeaderContentEncoding(t *testing.T) { 641 | url := fmt.Sprintf("/response/headers?header=%s&value=%s", HeaderContentEncoding, "htest") 642 | NewClient(t). 643 | To(ResponseHeadersServer). 644 | Get(url). 645 | Test(). 646 | HeaderContentEncoding("htest") 647 | } 648 | 649 | func TestResponse_HeaderContentLength(t *testing.T) { 650 | url := fmt.Sprintf("/response/headers?header=%s&value=%s", HeaderContentLength, "htest") 651 | NewClient(t). 652 | To(ResponseHeadersServer). 653 | Get(url). 654 | Test(). 655 | HeaderContentLength("htest") 656 | } 657 | 658 | func TestResponse_HeaderContentType(t *testing.T) { 659 | url := fmt.Sprintf("/response/headers?header=%s&value=%s", HeaderContentType, "htest") 660 | NewClient(t). 661 | To(ResponseHeadersServer). 662 | Get(url). 663 | Test(). 664 | HeaderContentType("htest") 665 | } 666 | 667 | func TestResponse_HeaderCookie(t *testing.T) { 668 | url := fmt.Sprintf("/response/headers?header=%s&value=%s", HeaderCookie, "htest") 669 | NewClient(t). 670 | To(ResponseHeadersServer). 671 | Get(url). 672 | Test(). 673 | HeaderCookie("htest") 674 | } 675 | 676 | func TestResponse_HeaderSetCookie(t *testing.T) { 677 | url := fmt.Sprintf("/response/headers?header=%s&value=%s", HeaderSetCookie, "htest") 678 | NewClient(t). 679 | To(ResponseHeadersServer). 680 | Get(url). 681 | Test(). 682 | HeaderSetCookie("htest") 683 | } 684 | 685 | func TestResponse_HeaderIfModifiedSince(t *testing.T) { 686 | url := fmt.Sprintf("/response/headers?header=%s&value=%s", HeaderIfModifiedSince, "htest") 687 | NewClient(t). 688 | To(ResponseHeadersServer). 689 | Get(url). 690 | Test(). 691 | HeaderIfModifiedSince("htest") 692 | } 693 | 694 | func TestResponse_HeaderLastModified(t *testing.T) { 695 | url := fmt.Sprintf("/response/headers?header=%s&value=%s", HeaderLastModified, "htest") 696 | NewClient(t). 697 | To(ResponseHeadersServer). 698 | Get(url). 699 | Test(). 700 | HeaderLastModified("htest") 701 | } 702 | 703 | func TestResponse_HeaderLocation(t *testing.T) { 704 | url := fmt.Sprintf("/response/headers?header=%s&value=%s", HeaderLocation, "htest") 705 | NewClient(t). 706 | To(ResponseHeadersServer). 707 | Get(url). 708 | Test(). 709 | HeaderLocation("htest") 710 | } 711 | 712 | func TestResponse_HeaderUpgrade(t *testing.T) { 713 | url := fmt.Sprintf("/response/headers?header=%s&value=%s", HeaderUpgrade, "htest") 714 | NewClient(t). 715 | To(ResponseHeadersServer). 716 | Get(url). 717 | Test(). 718 | HeaderUpgrade("htest") 719 | } 720 | 721 | func TestResponse_HeaderVary(t *testing.T) { 722 | url := fmt.Sprintf("/response/headers?header=%s&value=%s", HeaderVary, "htest") 723 | NewClient(t). 724 | To(ResponseHeadersServer). 725 | Get(url). 726 | Test(). 727 | HeaderVary("htest") 728 | } 729 | 730 | func TestResponse_HeaderWWWAuthenticate(t *testing.T) { 731 | url := fmt.Sprintf("/response/headers?header=%s&value=%s", HeaderWWWAuthenticate, "htest") 732 | NewClient(t). 733 | To(ResponseHeadersServer). 734 | Get(url). 735 | Test(). 736 | HeaderWWWAuthenticate("htest") 737 | } 738 | 739 | func TestResponse_HeaderXForwardedFor(t *testing.T) { 740 | url := fmt.Sprintf("/response/headers?header=%s&value=%s", HeaderXForwardedFor, "htest") 741 | NewClient(t). 742 | To(ResponseHeadersServer). 743 | Get(url). 744 | Test(). 745 | HeaderXForwardedFor("htest") 746 | } 747 | 748 | func TestResponse_HeaderXForwardedProto(t *testing.T) { 749 | url := fmt.Sprintf("/response/headers?header=%s&value=%s", HeaderXForwardedProto, "htest") 750 | NewClient(t). 751 | To(ResponseHeadersServer). 752 | Get(url). 753 | Test(). 754 | HeaderXForwardedProto("htest") 755 | } 756 | 757 | func TestResponse_HeaderXForwardedProtocol(t *testing.T) { 758 | url := fmt.Sprintf("/response/headers?header=%s&value=%s", HeaderXForwardedProtocol, "htest") 759 | NewClient(t). 760 | To(ResponseHeadersServer). 761 | Get(url). 762 | Test(). 763 | HeaderXForwardedProtocol("htest") 764 | } 765 | 766 | func TestResponse_HeaderXForwardedSsl(t *testing.T) { 767 | url := fmt.Sprintf("/response/headers?header=%s&value=%s", HeaderXForwardedSsl, "htest") 768 | NewClient(t). 769 | To(ResponseHeadersServer). 770 | Get(url). 771 | Test(). 772 | HeaderXForwardedSsl("htest") 773 | } 774 | 775 | func TestResponse_HeaderXUrlScheme(t *testing.T) { 776 | url := fmt.Sprintf("/response/headers?header=%s&value=%s", HeaderXUrlScheme, "htest") 777 | NewClient(t). 778 | To(ResponseHeadersServer). 779 | Get(url). 780 | Test(). 781 | HeaderXUrlScheme("htest") 782 | } 783 | 784 | func TestResponse_HeaderXHTTPMethodOverride(t *testing.T) { 785 | url := fmt.Sprintf("/response/headers?header=%s&value=%s", HeaderXHTTPMethodOverride, "htest") 786 | NewClient(t). 787 | To(ResponseHeadersServer). 788 | Get(url). 789 | Test(). 790 | HeaderXHTTPMethodOverride("htest") 791 | } 792 | 793 | func TestResponse_HeaderXRealIP(t *testing.T) { 794 | url := fmt.Sprintf("/response/headers?header=%s&value=%s", HeaderXRealIP, "htest") 795 | NewClient(t). 796 | To(ResponseHeadersServer). 797 | Get(url). 798 | Test(). 799 | HeaderXRealIP("htest") 800 | } 801 | 802 | func TestResponse_HeaderXRequestID(t *testing.T) { 803 | url := fmt.Sprintf("/response/headers?header=%s&value=%s", HeaderXRequestID, "htest") 804 | NewClient(t). 805 | To(ResponseHeadersServer). 806 | Get(url). 807 | Test(). 808 | HeaderXRequestID("htest") 809 | } 810 | 811 | func TestResponse_HeaderServer(t *testing.T) { 812 | url := fmt.Sprintf("/response/headers?header=%s&value=%s", HeaderServer, "htest") 813 | NewClient(t). 814 | To(ResponseHeadersServer). 815 | Get(url). 816 | Test(). 817 | HeaderServer("htest") 818 | } 819 | 820 | func TestResponse_HeaderOrigin(t *testing.T) { 821 | url := fmt.Sprintf("/response/headers?header=%s&value=%s", HeaderOrigin, "htest") 822 | NewClient(t). 823 | To(ResponseHeadersServer). 824 | Get(url). 825 | Test(). 826 | HeaderOrigin("htest") 827 | } 828 | 829 | func TestResponse_HeaderAccessControlRequestMethod(t *testing.T) { 830 | url := fmt.Sprintf("/response/headers?header=%s&value=%s", HeaderAccessControlRequestMethod, "htest") 831 | NewClient(t). 832 | To(ResponseHeadersServer). 833 | Get(url). 834 | Test(). 835 | HeaderAccessControlRequestMethod("htest") 836 | } 837 | 838 | func TestResponse_HeaderAccessControlRequestHeaders(t *testing.T) { 839 | url := fmt.Sprintf("/response/headers?header=%s&value=%s", HeaderAccessControlRequestHeaders, "htest") 840 | NewClient(t). 841 | To(ResponseHeadersServer). 842 | Get(url). 843 | Test(). 844 | HeaderAccessControlRequestHeaders("htest") 845 | } 846 | 847 | func TestResponse_HeaderAccessControlAllowOrigin(t *testing.T) { 848 | url := fmt.Sprintf("/response/headers?header=%s&value=%s", HeaderAccessControlAllowOrigin, "htest") 849 | NewClient(t). 850 | To(ResponseHeadersServer). 851 | Get(url). 852 | Test(). 853 | HeaderAccessControlAllowOrigin("htest") 854 | } 855 | 856 | func TestResponse_HeaderAccessControlAllowMethods(t *testing.T) { 857 | url := fmt.Sprintf("/response/headers?header=%s&value=%s", HeaderAccessControlAllowMethods, "htest") 858 | NewClient(t). 859 | To(ResponseHeadersServer). 860 | Get(url). 861 | Test(). 862 | HeaderAccessControlAllowMethods("htest") 863 | } 864 | 865 | func TestResponse_HeaderAccessControlAllowHeaders(t *testing.T) { 866 | url := fmt.Sprintf("/response/headers?header=%s&value=%s", HeaderAccessControlAllowHeaders, "htest") 867 | NewClient(t). 868 | To(ResponseHeadersServer). 869 | Get(url). 870 | Test(). 871 | HeaderAccessControlAllowHeaders("htest") 872 | } 873 | 874 | func TestResponse_HeaderAccessControlAllowCredentials(t *testing.T) { 875 | url := fmt.Sprintf("/response/headers?header=%s&value=%s", HeaderAccessControlAllowCredentials, "htest") 876 | NewClient(t). 877 | To(ResponseHeadersServer). 878 | Get(url). 879 | Test(). 880 | HeaderAccessControlAllowCredentials("htest") 881 | } 882 | 883 | func TestResponse_HeaderAccessControlExposeHeaders(t *testing.T) { 884 | url := fmt.Sprintf("/response/headers?header=%s&value=%s", HeaderAccessControlExposeHeaders, "htest") 885 | NewClient(t). 886 | To(ResponseHeadersServer). 887 | Get(url). 888 | Test(). 889 | HeaderAccessControlExposeHeaders("htest") 890 | } 891 | 892 | func TestResponse_HeaderAccessControlMaxAge(t *testing.T) { 893 | url := fmt.Sprintf("/response/headers?header=%s&value=%s", HeaderAccessControlMaxAge, "htest") 894 | NewClient(t). 895 | To(ResponseHeadersServer). 896 | Get(url). 897 | Test(). 898 | HeaderAccessControlMaxAge("htest") 899 | } 900 | 901 | func TestResponse_HeaderStrictTransportSecurity(t *testing.T) { 902 | url := fmt.Sprintf("/response/headers?header=%s&value=%s", HeaderStrictTransportSecurity, "htest") 903 | NewClient(t). 904 | To(ResponseHeadersServer). 905 | Get(url). 906 | Test(). 907 | HeaderStrictTransportSecurity("htest") 908 | } 909 | 910 | func TestResponse_HeaderXContentTypeOptions(t *testing.T) { 911 | url := fmt.Sprintf("/response/headers?header=%s&value=%s", HeaderXContentTypeOptions, "htest") 912 | NewClient(t). 913 | To(ResponseHeadersServer). 914 | Get(url). 915 | Test(). 916 | HeaderXContentTypeOptions("htest") 917 | } 918 | 919 | func TestResponse_HeaderXXSSProtection(t *testing.T) { 920 | url := fmt.Sprintf("/response/headers?header=%s&value=%s", HeaderXXSSProtection, "htest") 921 | NewClient(t). 922 | To(ResponseHeadersServer). 923 | Get(url). 924 | Test(). 925 | HeaderXXSSProtection("htest") 926 | } 927 | 928 | func TestResponse_HeaderXFrameOptions(t *testing.T) { 929 | url := fmt.Sprintf("/response/headers?header=%s&value=%s", HeaderXFrameOptions, "htest") 930 | NewClient(t). 931 | To(ResponseHeadersServer). 932 | Get(url). 933 | Test(). 934 | HeaderXFrameOptions("htest") 935 | } 936 | 937 | func TestResponse_HeaderContentSecurityPolicy(t *testing.T) { 938 | url := fmt.Sprintf("/response/headers?header=%s&value=%s", HeaderContentSecurityPolicy, "htest") 939 | NewClient(t). 940 | To(ResponseHeadersServer). 941 | Get(url). 942 | Test(). 943 | HeaderContentSecurityPolicy("htest") 944 | } 945 | 946 | func TestResponse_HeaderXCSRFToken(t *testing.T) { 947 | url := fmt.Sprintf("/response/headers?header=%s&value=%s", HeaderXCSRFToken, "htest") 948 | NewClient(t). 949 | To(ResponseHeadersServer). 950 | Get(url). 951 | Test(). 952 | HeaderXCSRFToken("htest") 953 | } 954 | 955 | func StatusHandler(w http.ResponseWriter, req *http.Request) { 956 | codeStr := chi.URLParam(req, "code") 957 | code, err := strconv.Atoi(codeStr) 958 | if err != nil { 959 | w.WriteHeader(http.StatusNotFound) 960 | return 961 | } 962 | w.WriteHeader(code) 963 | } 964 | 965 | func HeadersHandler(w http.ResponseWriter, req *http.Request) { 966 | query := req.URL.Query() 967 | header := query.Get("header") 968 | value := query.Get("value") 969 | w.Header().Set(header, value) 970 | } 971 | -------------------------------------------------------------------------------- /server_test.go: -------------------------------------------------------------------------------- 1 | package htest 2 | 3 | import ( 4 | "github.com/go-chi/chi" 5 | "io" 6 | "net/http" 7 | ) 8 | 9 | var ( 10 | Mux *chi.Mux 11 | ) 12 | 13 | func init() { 14 | Mux = chi.NewRouter() 15 | Mux.Get("/name", NameHandler) 16 | Mux.Get("/client/get", NameHandler) 17 | Mux.Trace("/client/trace", NameHandler) 18 | Mux.Delete("/client/delete", NameHandler) 19 | Mux.Connect("/client/connect", NameHandler) 20 | Mux.Options("/client/options", NameHandler) 21 | Mux.Head("/client/head", NameHandler) 22 | Mux.Post("/client/post", ClientDataHandler) 23 | Mux.Put("/client/put", ClientDataHandler) 24 | Mux.Patch("/client/patch", ClientDataHandler) 25 | Mux.Post("/client/patch", ClientDataHandler) 26 | Mux.Get("/request/header", HeaderHandler) 27 | Mux.Get("/request/cookie", CookieHandler) 28 | Mux.Get("/body/user", UserDataHandler) 29 | Mux.Get("/xml_body/user", UserDataXMLHandler) 30 | } 31 | 32 | func NameHandler(w http.ResponseWriter, req *http.Request) { 33 | io.WriteString(w, `{"name": "hexi"}`) 34 | } 35 | --------------------------------------------------------------------------------