Skip to content

Commit

Permalink
feat: adds check to throw if http status is not 200 (#78)
Browse files Browse the repository at this point in the history
BREAKING CHANGE
  • Loading branch information
jacktuck committed May 31, 2021
1 parent 2033ae5 commit 4b6e032
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 5 deletions.
16 changes: 15 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,24 @@ async function getPage (url: string, opts: Opts) {
const contentType = res.headers.get('Content-Type')
const contentLength = res.headers.get('Content-Length')

if (res.status !== 200) {
throw new UnexpectedError({
...UnexpectedError.BAD_HTTP_STATUS,
info: {
url,
httpStatus: res.status
}
})
}

if (/text\/html|application\/xhtml+xml/.test(contentType) === false) {
throw new UnexpectedError({
...UnexpectedError.EXPECTED_HTML,
info: { contentType, contentLength }
info: {
url,
contentType,
contentLength
}
})
}

Expand Down
8 changes: 7 additions & 1 deletion src/unexpectedError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@ export default class UnexpectedError extends Error {
name: 'BAD_OPTIONS'
}

static BAD_HTTP_STATUS = {
message: 'Error in http request (http status not OK)',
name: 'BAD_HTTP_STATUS'
}

info: {
contentLength?: number,
contentType?: string
contentType?: string,
httpStatus?: string
}

constructor (errorType: { message: string, name: string, info? }) {
Expand Down
2 changes: 1 addition & 1 deletion test/basic/test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { unfurl } from '../../src/'
import nock from 'nock'

test('should handle content which is excaped badly', async () => {
test('should handle content which is escaped badly', async () => {
nock('http://localhost')
.get('/html/double-escaped-edge-case')
.replyWithFile(200, __dirname + '/double escaped-edge-case.html', {
Expand Down
2 changes: 0 additions & 2 deletions test/encoding/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ test('should detect GB2312 charset (HTML 4) and convert to UTF-8', async () => {
.get('/html4/gb2312')
.replyWithFile(200, __dirname + '/html_4.html', { 'Content-Type': 'text/html' })

// require('http').get('http://localhost/html4/gb2312', console.log)

const result = await unfurl('http://localhost/html4/gb2312')

const expected = {
Expand Down
26 changes: 26 additions & 0 deletions test/general/status-code.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { unfurl } from '../../src'
import nock from 'nock'
import UnexpectedError from '../../src/unexpectedError'

test('should throw if status code not 200', () => {
nock('http://localhost')
.get('/html/return-404')
.reply(404)

return expect(unfurl('http://localhost/html/return-404'))
.rejects
.toThrow(new UnexpectedError(UnexpectedError.BAD_HTTP_STATUS))
})

test('should not throw if status code is 200', async () => {
nock('http://localhost')
.get('/html/return-200')
.reply(200, '', {
'Content-Type': 'text/html'
})

return expect(unfurl('http://localhost/html/return-200'))
.resolves
.toBeTruthy()
})

1 comment on commit 4b6e032

@jacktuck
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixes #77

Please sign in to comment.