Skip to content

Commit

Permalink
added nock for better mocking
Browse files Browse the repository at this point in the history
  • Loading branch information
jt committed Sep 25, 2018
1 parent 947cd08 commit 97563b8
Show file tree
Hide file tree
Showing 13 changed files with 322 additions and 383 deletions.
87 changes: 87 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"main": "dist/index.js",
"dependencies": {
"debug": "^3.1.0",
"he": "^1.2.0",
"htmlparser2": "^3.9.2",
"iconv-lite": "^0.4.24",
"node-fetch": "^2.2.0",
Expand All @@ -25,6 +26,7 @@
"eslint-plugin-standard": "^4.0.0",
"eslint-plugin-typescript": "^0.12.0",
"jest": "^23.6.0",
"nock": "^10.0.0",
"ts-jest": "^23.1.4",
"tsc": "^1.20150623.0",
"tslint": "^5.11.0",
Expand Down
10 changes: 5 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
} from './schema'

import { Metadata, Opts } from './types'
import he from 'he'

function unfurl (url: string, opts?: Opts): Promise<Metadata> {
if (opts === undefined) {
Expand Down Expand Up @@ -280,8 +281,6 @@ function getMetadata (ctx, opts: Opts) {
parser.reset()
}
}
}, {
decodeEntities: true
})

parser.write(text)
Expand Down Expand Up @@ -311,9 +310,10 @@ function parse (ctx) {
continue
}

if (item.type === 'string') {
metaValue = metaValue.toString()
} else if (item.type === 'number') {
// decoding html entities
metaValue = he.decode(he.decode(metaValue.toString()))

if (item.type === 'number') {
metaValue = parseInt(metaValue, 10)
} else if (item.type === 'url') {
metaValue = resolveUrl(ctx.url, metaValue)
Expand Down
11 changes: 11 additions & 0 deletions test/basic/double escaped-edge-case.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="description" content="&amp;quot;" />
</head>
<body></body>
</html>


52 changes: 41 additions & 11 deletions test/basic/test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,35 @@
const unfurl = require('../../src/')
import TestServer from '../server'
import nock from 'nock'
test('should detect title, description and keywords', async () => {
nock('http://localhost')
.get('/html/basic')
.replyWithFile(200, __dirname + '/basic.html', {
'Content-Type': 'text/html'
})

const result = await unfurl('http://localhost/html/basic')

const port = process.env.port
const baseUrl = `http://localhost:${port}`
const expected = {
favicon: 'http://localhost/favicon.ico',
description: 'aaa',
keywords: ['a', 'b', 'c'],
title: 'ccc'
}

beforeAll(then => TestServer.listen(port, then))
afterAll(then => TestServer.close(then))
expect(result).toEqual(expected)
})

test('should detect title, description and keywords', async () => {
const result = await unfurl(baseUrl + '/html/basic')
nock('http://localhost')
.get('/html/basic')
.replyWithFile(200, __dirname + '/basic.html', {
'Content-Type': 'text/html'
})

const result = await unfurl('http://localhost/html/basic')

const expected = {
favicon: 'http://localhost:9000/favicon.ico',
favicon: 'http://localhost/favicon.ico',
description: 'aaa',
keywords: ['a', 'b', 'c'],
title: 'ccc'
Expand All @@ -21,10 +39,16 @@ test('should detect title, description and keywords', async () => {
})

test('should detect last dupe of title, description and keywords', async () => {
const result = await unfurl(baseUrl + '/html/basic-duplicates')
nock('http://localhost')
.get('/html/basic-duplicates')
.replyWithFile(200, __dirname + '/basic-duplicates.html', {
'Content-Type': 'text/html'
})

const result = await unfurl('http://localhost/html/basic-duplicates')

const expected = {
favicon: 'http://localhost:9000/favicon.ico',
favicon: 'http://localhost/favicon.ico',
description: 'aaa',
keywords: ['a', 'b', 'c'],
title: 'ccc'
Expand All @@ -34,10 +58,16 @@ test('should detect last dupe of title, description and keywords', async () => {
})

test('should detect last dupe of title, description and keywords', async () => {
const result = await unfurl(baseUrl + '/html/keyword-edge-cases')
nock('http://localhost')
.get('/html/keyword-edge-cases')
.replyWithFile(200, __dirname + '/keyword-edge-cases.html', {
'Content-Type': 'text/html'
})

const result = await unfurl('http://localhost/html/keyword-edge-cases')

const expected = {
favicon: 'http://localhost:9000/favicon.ico',
favicon: 'http://localhost/favicon.ico',
keywords: ['foo', 'bar', 'baz quix', 'foo', 'foo']
}

Expand Down
30 changes: 20 additions & 10 deletions test/encoding/test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
const unfurl = require('../../src/')
import TestServer from '../server'

const port = process.env.port
const baseUrl = `http://localhost:${port}`

beforeAll(then => TestServer.listen(port, then))
afterAll(then => TestServer.close(then))
import fs from 'fs'
import nock from 'nock'

test('should detect GB2312 charset (HTML 4) and convert to UTF-8', async () => {
const result = await unfurl(baseUrl + '/encoding/html/4')
nock('http://localhost')
.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 = {
description: '腾讯网从2003年创立至今,已经成为集新闻信息,区域垂直生活服务、社会化媒体资讯和产品为一体的互联网媒体平台。腾讯网下设新闻、科技、财经、娱乐、体育、汽车、时尚等多个频道,充分满足用户对不同类型资讯的需求。同时专注不同领域内容,打造精品栏目,并顺应技术发展趋势,推出网络直播等创新形式,改变了用户获取资讯的方式和习惯。',
Expand All @@ -20,7 +22,11 @@ test('should detect GB2312 charset (HTML 4) and convert to UTF-8', async () => {
})

test('should detect GB2312 charset (HTML 5) and convert to UTF-8', async () => {
const result = await unfurl(baseUrl + '/encoding/html/5')
nock('http://localhost')
.get('/html5/gb2312')
.replyWithFile(200, __dirname + '/html_5.html', { 'Content-Type': 'text/html' })

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

const expected = {
description: '腾讯网从2003年创立至今,已经成为集新闻信息,区域垂直生活服务、社会化媒体资讯和产品为一体的互联网媒体平台。腾讯网下设新闻、科技、财经、娱乐、体育、汽车、时尚等多个频道,充分满足用户对不同类型资讯的需求。同时专注不同领域内容,打造精品栏目,并顺应技术发展趋势,推出网络直播等创新形式,改变了用户获取资讯的方式和习惯。',
Expand All @@ -32,11 +38,15 @@ test('should detect GB2312 charset (HTML 5) and convert to UTF-8', async () => {
})

test('should detect EUC-JP charset (HTML 5) and convert to UTF-8', async () => {
const result = await unfurl(baseUrl + '/encoding/html/euc-jp')
nock('http://localhost')
.get('/html5/euc-jp')
.replyWithFile(200, __dirname + '/euc-jp.html', { 'Content-Type': 'text/html' })

const result = await unfurl('http://localhost/html5/euc-jp')

const expected = {
description: '楽天市場はインターネット通販が楽しめる総合ショッピングモール。楽天スーパーポイントがどんどん貯まる!使える!毎日お得なクーポンも。あす楽利用で翌日にお届け。食品から家電、ファッション、ベビー用品、コスメまで、充実の品揃え。',
favicon: 'http://localhost:9000/favicon.ico',
favicon: 'http://localhost/favicon.ico',
keywords: ['通販', 'インターネット通販', 'オンラインショッピング', '楽天', '楽天市場', 'rakuten', 'ラクテン', 'らくてん'],
open_graph: {
description: '楽天市場はインターネット通販が楽しめる総合ショッピングモール。楽天スーパーポイントがどんどん貯まる!使える!毎日お得なクーポンも。あす楽利用で翌日にお届け。食品から家電、ファッション、ベビー用品、コスメまで、充実の品揃え。',
Expand Down
15 changes: 8 additions & 7 deletions test/general/content-type.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import fs from 'fs'
import http from 'http'
import iconv from 'iconv-lite'
import TestServer from '../server'

const unfurl = require('../../src/')
import UnexpectedError from '../../src/unexpectedError'

const port = process.env.port
const baseUrl = `http://localhost:${port}`

beforeAll(then => TestServer.listen(port, then))
afterAll(then => TestServer.close(then))
import nock from 'nock'

test('should throw bad content type error', async () => {
try {
await unfurl(baseUrl + '/image')
nock('http://localhost')
.get('/image')
.reply(200, '', {
'Content-Type': 'image/png'
})

await unfurl('http://localhost/image')
} catch (err) {
expect(err.name).toEqual(UnexpectedError.EXPECTED_HTML.name)
expect(err.message).toEqual(UnexpectedError.EXPECTED_HTML.message)
Expand Down

0 comments on commit 97563b8

Please sign in to comment.