<commit_before>import test from 'ava'
import nock from 'nock'
import data from './fixtures/data'
import Onionoo from '../'

test('Handle HTML responses for errors', async t => {
  const onionoo = new Onionoo()

  const defaultEndpoint = data.defaultEndpoints[0]
  const scope = nock(data.defaultBaseUrl)
    .get(`/${defaultEndpoint}`)
    .reply(400, data.dummy400Response)

  try {
    await onionoo[defaultEndpoint]()
  } catch (e) {
    t.deepEqual(e.response.body, data.dummy400Response)
  }

  t.truthy(scope.isDone())
})<commit_msg>Test useful errors for HTTP response codes<commit_after>import test from 'ava'
import nock from 'nock'
import data from './fixtures/data'
import Onionoo from '../'

test('Handle HTML responses for errors', async t => {
  const onionoo = new Onionoo()

  const defaultEndpoint = data.defaultEndpoints[0]
  const scope = nock(data.defaultBaseUrl)
    .get(`/${defaultEndpoint}`)
    .reply(400, data.dummy400Response)

  try {
    await onionoo[defaultEndpoint]()
  } catch (e) {
    t.deepEqual(e.response.body, data.dummy400Response)
  }

  t.truthy(scope.isDone())
})

test('Throw useful errors for HTTP response codes', async t => {
  const onionoo = new Onionoo()

  const defaultEndpoint = data.defaultEndpoints[0]
  const responseCodes = {
    400: 'Bad Request',
    404: 'Not Found',
    500: 'Internal Server Error',
    503: 'Service Unavailable'
  }

  for (const responseCode in responseCodes) {
    const scope = nock(data.defaultBaseUrl)
      .get(`/${defaultEndpoint}`)
      .reply(responseCode)

    try {
      await onionoo[defaultEndpoint]()
    } catch (e) {
      t.is(e.message, `Response code ${responseCode} (${responseCodes[responseCode]})`)
      t.is(e.statusCode, parseInt(responseCode, 10))
      t.is(e.statusMessage, responseCodes[responseCode])
    }
    t.truthy(scope.isDone())
  }
})
{'fraction_non_alphanumeric': 0.13455328310010764, 'fraction_numerical': 0.01883745963401507, 'mean_word_length': 3.7666666666666666, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1947215', 'n_tokens_mistral': 630, 'n_tokens_neox': 603, 'n_words': 149}