-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathurl.test.js
71 lines (64 loc) · 1.79 KB
/
url.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const app = require('../app').app
const mongoose = require('mongoose')
const request = require('supertest')
const UrlModel = require('../app/models/UrlShortner')
const HttpStatus = require('http-status-codes')
const redis = require('../config/redis').redisClient
const testUrl = 'http://codeuino.org/codeofconduct'
// let shortUrl = ''
let server
/**
* This will pe performed once at the beginning of the test
*/
beforeAll(async (done) => {
await UrlModel.deleteMany()
await redis.flushall()
server = app.listen(4000, () => {
global.agent = request.agent(server)
done()
})
})
/**
* Testing Shorten URL
*/
test('Should short the URL', async (done) => {
const response = await request(app)
.post('/shortUrl/shorten')
.send({
longUrl: `${testUrl}`
})
.expect(HttpStatus.CREATED)
// Assert that db was changed
const url = await UrlModel.findById(response.body._id)
expect(url).not.toBeNull()
// shortUrl = response.body.shortUrl
// Assertions about the
expect(response.body).toMatchObject({
longUrl: `${testUrl}`,
shortUrl: `${response.body.shortUrl}`,
urlCode: `${response.body.urlCode}`
})
done()
})
/**
* ShortURL to longUrl
*/
// test('Should redirect to the longUrl ', async (done) => {
// const param = shortUrl.toString().split('/')[1]
// shortUrl = 'http://localhost:4000' + '/' + param
// console.log('ShortUrl ', shortUrl)
// await request(app)
// .get(`${shortUrl}`)
// .expect(301, `${testUrl}`)
// done()
// })
afterAll(async () => {
// avoid jest open handle error
await new Promise((resolve) => setTimeout(() => resolve(), 500))
// close server
await server.close()
// flush all
await redis.flushall()
// Closing the DB connection allows Jest to exit successfully.
await mongoose.connection.close()
})