{"action":"created","issue":{"url":"https://api.github.com/repos/apiaryio/dredd/issues/556","repository_url":"https://api.github.com/repos/apiaryio/dredd","labels_url":"https://api.github.com/repos/apiaryio/dredd/issues/556/labels{/name}","comments_url":"https://api.github.com/repos/apiaryio/dredd/issues/556/comments","events_url":"https://api.github.com/repos/apiaryio/dredd/issues/556/events","html_url":"https://github.com/apiaryio/dredd/issues/556","id":164793285,"number":556,"title":"Do not parse and validate body if no expectations are set and response content-length is zero or not present","user":{"login":"netmilk","id":79609,"avatar_url":"https://avatars3.githubusercontent.com/u/79609?v=4","gravatar_id":"","url":"https://api.github.com/users/netmilk","html_url":"https://github.com/netmilk","followers_url":"https://api.github.com/users/netmilk/followers","following_url":"https://api.github.com/users/netmilk/following{/other_user}","gists_url":"https://api.github.com/users/netmilk/gists{/gist_id}","starred_url":"https://api.github.com/users/netmilk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/netmilk/subscriptions","organizations_url":"https://api.github.com/users/netmilk/orgs","repos_url":"https://api.github.com/users/netmilk/repos","events_url":"https://api.github.com/users/netmilk/events{/privacy}","received_events_url":"https://api.github.com/users/netmilk/received_events","type":"User","site_admin":false},"labels":[{"id":583152477,"url":"https://api.github.com/repos/apiaryio/dredd/labels/Epic:%20empty%20responses%20and%20binary%20files","name":"Epic: empty responses and binary files","color":"C6BA48","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":21,"created_at":"2016-07-11T09:17:54Z","updated_at":"2017-11-10T16:18:19Z","closed_at":null,"author_association":"MEMBER","body":"I propose to do not try to parse and validate the body response in case the response `content-length` header value is `0` and the expected `body` or `bodySchema` is empty.\n\nAt the moment when your API response has JSON content type (`application/json` etc) but empty body (empty string - zero characters) Dredd tries to parse the body and it results slightly not understandable error `fail: body: Real body \"Content-Type\" header is \"application/json\" but body is not a parseble JSON.`\n\nThe zero `content-length` header is indicating the intent to do not send any data and we shouldn't expect any and assume it's ok.\n\nHere is a temporary workaround solution with user of hooks:\n\n``` js\nvar hooks = require('hooks');\n\n\n// Do not parse and validate response body if the expected body and schema is empty and the response \n// header content-type is zero or not present regardless the response content-type\nhooks.beforeEachValidation(function(transaction){\n  var body = transaction['expected']['body'];\n  var bodySchema = transaction['expected']['bodySchema'];\n\n  var isBodyEmpty = body == null || body == undefined || body == ''\n  var isBodySchemaEmpty = bodySchema == null || bodySchema == undefined || bodySchema == ''\n\n  if(isBodyEmpty && isBodySchemaEmpty){\n\n    //caseless lookup for the content-length header key\n    var contentLengthKey = null;\n    var responseHeaderKeys = Object.keys(transaction['real']['headers']);\n    responseHeaderKeys.forEach(function(responseHeaderKey){\n      if(responseHeaderKey.toLowerCase() == 'content-length'){\n        contentTypeKey = responseHeaderKey;\n      };\n    });\n\n    var contentLength = null;\n    // if response has content-length\n    if(contentLengthKey !== null){\n      var contentLength = parseInt(transacation['real']['headers'][contetLengthKey],10);\n    }\n\n    if(contentLengthKey === null || conetntLength === 0){\n      //skip the body validation the way mentioned in https://github.com/apiaryio/dredd/issues/510\n      hooks.log(\"skipping body validation\")\n      transaction['expected']['body'] = undefined;\n      transaction['expected']['bodySchema'] = undefined;\n    };\n  };\n});\n```\n"},"comment":{"url":"https://api.github.com/repos/apiaryio/dredd/issues/comments/343517147","html_url":"https://github.com/apiaryio/dredd/issues/556#issuecomment-343517147","issue_url":"https://api.github.com/repos/apiaryio/dredd/issues/556","id":343517147,"user":{"login":"honzajavorek","id":283441,"avatar_url":"https://avatars0.githubusercontent.com/u/283441?v=4","gravatar_id":"","url":"https://api.github.com/users/honzajavorek","html_url":"https://github.com/honzajavorek","followers_url":"https://api.github.com/users/honzajavorek/followers","following_url":"https://api.github.com/users/honzajavorek/following{/other_user}","gists_url":"https://api.github.com/users/honzajavorek/gists{/gist_id}","starred_url":"https://api.github.com/users/honzajavorek/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/honzajavorek/subscriptions","organizations_url":"https://api.github.com/users/honzajavorek/orgs","repos_url":"https://api.github.com/users/honzajavorek/repos","events_url":"https://api.github.com/users/honzajavorek/events{/privacy}","received_events_url":"https://api.github.com/users/honzajavorek/received_events","type":"User","site_admin":false},"created_at":"2017-11-10T16:18:19Z","updated_at":"2017-11-10T16:18:19Z","author_association":"MEMBER","body":"I just found out [Gavel.js](https://github.com/apiaryio/gavel.js/) isn't able to validate primitive types like this:\r\n\r\n```json\r\n{\"type\": \"string\", \"enum\": [\"\"]}\r\n```\r\n\r\nThat means either\r\n\r\n- that there will be no way to force validation of the empty response,\r\n- or that we need to figure out how Gavel could support such use case,\r\n- or that we need to figure out a different way user could signalize they want to force validation of the empty response.\r\n\r\n🤔 "}}
{"actor":{"display_login":"honzajavorek"}}