stretchr / testify

A toolkit with common assertions and mocks that plays nicely with the standard library
MIT License
22.51k stars 1.56k forks source link

Assert for HTTP Headers #598

Open falkerson opened 6 years ago

falkerson commented 6 years ago

Assert package has 2 useful method to verify HTTP status - https://godoc.org/github.com/stretchr/testify/assert#HTTPSuccess and body - https://godoc.org/github.com/stretchr/testify/assert#HTTPBodyContains . It will be nice also to add method to verify HTTP headers.

devdinu commented 6 years ago

@falkerson Agreed, You could raise a PR. Share the func signature you think gonna be useful, so we could get comments.

falkerson commented 6 years ago

@devdinu pls take a look https://github.com/stretchr/testify/pull/601

devdinu commented 6 years ago

I've been thinking an alternate or a function which takes request, coz these functions doesn't accept body too.

Also for each type of extraction we've to keep exposing a function. Rather

r, err := http.NewRequest(method, url, body)
resp, err := assert.HTTPSuccess(r)
assert.Equal(t, "some-data", resp.Body.String())
assert.True(t, resp.Header.Contains("key", "val"))
...

it would be a new set of functions, probably others could comment, because i haven't used much of HTTP functions, as i write those custom. These function has more params and feels like just a wrapper around making request and parsing or reading headers (which could be done by a struct).

mattgen88 commented 6 years ago

The usefulness of the HTTP functions seems to fall a bit short. If your handler requires reading headers, they won't work for you. If you need to send something in the body, same problem.

KrzysztofMadejski commented 4 years ago

The project I'm working on have quite a lot of tests for http handlers. I was thinking as well to create an abstraction layer to make tests definitions simpler. What do you think about below approach?

func TestListJobs_CreatJob(t *testing.T) {
  // on the first line we define what we call
  t.Run("List: no jobs", call(ListJobsHandler()).GET("/jobs?sort=-date")
  // then what we expect
    .Assert().Status(httpStatusOK).ContentType("application/json").Header("Allow-Origin: *").JsonBody(`[]`)).JsonConformsTo([]*models.Job)

  // similarly for a POST 
  t.Run("POST a job", call(CreateJobHandler()).POST("/jobs").JsonBody(`{}`).SetContextValue(UsernameKey, username)
    .Assert() // etc
  )
}

So I guess the addition would be to allow chaining to offer more flexibility to create request and not to repeat code, and also to add new methods for checking results.

UPDATE: I moved my comment to a new issue #809

KrzysztofMadejski commented 4 years ago

I'm working on my proposition here: https://github.com/KrzysztofMadejski/testify-handlers

KrzysztofMadejski commented 4 years ago

@mattgen88 @falkerson @devdinu You could try my lib for testing http handlers: https://gitlab.com/gotests/handlertest

Matovidlo commented 12 months ago

@devdinu Would it be good idea that I create PR to support override of HTTP headers and HTTP body which is not supported?

I would like to use only this library to test HTTP handlers and do not include n testing frameworks for n purposes.