emicklei / forest

REST Api Testing package for writing integration tests in Go
MIT License
123 stars 8 forks source link

RequestConfig.Content doesn't properly handle JSON string #2

Closed mmindenhall closed 9 years ago

mmindenhall commented 9 years ago

Hello, The RequestConfig.Content method does not detect when the payload param is already a JSON string, and therefore does not need to be marshaled. For example:

var testJson = `{
   "email": "vizzini@example.com",
   "first_name": "Boss",
   "last_name": "Vizzini",
}`
    resp := local.POST(t, Path("/api/v1/platform/:resource_name", "user").Content(testUser1, "application/json"))

// results in string being escaped by json.Marshal, which fails to Unmarshal:
// "{\n   \"email\": \"vizzini@example.com\",\n   \"first_name\": \"Boss\",\n   \"last_name\": \"Vizzini\",\n}"

See example in go playground.

emicklei commented 9 years ago

Thank you for sharing this.

The Content function is indeed a bit confusing here. The current way to pass in a JSON as is, is to set the BodyReader field of the config. This, however, is quite different from the function api.

I propose to change this function (Content) such that is works as expected from your example. In addition, I will implement an extra validation to verify that the content is correct JSON (or XML if passed "application/xml").

emicklei commented 9 years ago

for now, I leave out the validation part until someone requests for it.