|
| 1 | +import { yoga } from '../yoga' |
| 2 | +import { createServer, Server } from 'http' |
| 3 | +import { AddressInfo } from 'net' |
| 4 | +import { fetch, File, FormData } from '@whatwg-node/fetch' |
| 5 | +import * as fs from 'fs' |
| 6 | +import * as crypto from 'crypto' |
| 7 | +import * as path from 'path' |
| 8 | + |
| 9 | +function md5File(path: string) { |
| 10 | + return new Promise((resolve, reject) => { |
| 11 | + const output = crypto.createHash('md5') |
| 12 | + const input = fs.createReadStream(path) |
| 13 | + |
| 14 | + input.on('error', (err) => { |
| 15 | + reject(err) |
| 16 | + }) |
| 17 | + |
| 18 | + output.once('readable', () => { |
| 19 | + resolve(output.read().toString('hex')) |
| 20 | + }) |
| 21 | + |
| 22 | + input.pipe(output) |
| 23 | + }) |
| 24 | +} |
| 25 | + |
| 26 | +describe('graphql-auth example integration', () => { |
| 27 | + let server: Server |
| 28 | + let port: number |
| 29 | + |
| 30 | + beforeAll(async () => { |
| 31 | + server = createServer(yoga) |
| 32 | + await new Promise<void>((resolve) => server.listen(0, resolve)) |
| 33 | + port = (server.address() as AddressInfo).port |
| 34 | + }) |
| 35 | + |
| 36 | + afterAll(async () => { |
| 37 | + await new Promise((resolve) => server.close(resolve)) |
| 38 | + }) |
| 39 | + |
| 40 | + it('should execute query', async () => { |
| 41 | + const response = await fetch( |
| 42 | + `http://localhost:${port}/graphql?query=query{hello}`, |
| 43 | + ) |
| 44 | + const body = await response.json() |
| 45 | + expect(body.errors).toBeUndefined() |
| 46 | + expect(body.data).toEqual({ |
| 47 | + hello: 'Hello World', |
| 48 | + }) |
| 49 | + }) |
| 50 | + |
| 51 | + it('should save file', async () => { |
| 52 | + const sourceFilePath = path.join( |
| 53 | + __dirname, |
| 54 | + '..', |
| 55 | + '..', |
| 56 | + '..', |
| 57 | + 'website', |
| 58 | + 'public', |
| 59 | + 'logo.png', |
| 60 | + ) |
| 61 | + const sourceMd5 = await md5File(sourceFilePath) |
| 62 | + |
| 63 | + const formData = new FormData() |
| 64 | + formData.set( |
| 65 | + 'operations', |
| 66 | + JSON.stringify({ |
| 67 | + query: /* GraphQL */ ` |
| 68 | + mutation saveFile($file: File!) { |
| 69 | + saveFile(file: $file) |
| 70 | + } |
| 71 | + `, |
| 72 | + }), |
| 73 | + ) |
| 74 | + formData.set('map', JSON.stringify({ 0: ['variables.file'] })) |
| 75 | + formData.set( |
| 76 | + '0', |
| 77 | + new File( |
| 78 | + [await fs.promises.readFile(sourceFilePath)], |
| 79 | + path.basename(sourceFilePath), |
| 80 | + { type: 'image/png' }, |
| 81 | + ), |
| 82 | + ) |
| 83 | + |
| 84 | + const response = await fetch(`http://localhost:${port}/graphql`, { |
| 85 | + method: 'POST', |
| 86 | + body: formData, |
| 87 | + }) |
| 88 | + |
| 89 | + const body = await response.json() |
| 90 | + expect(body.errors).toBeUndefined() |
| 91 | + expect(body.data).toEqual({ |
| 92 | + saveFile: true, |
| 93 | + }) |
| 94 | + |
| 95 | + const targetFilePath = path.join(__dirname, '..', 'logo.png') |
| 96 | + await fs.promises.stat(targetFilePath) |
| 97 | + const targetMd5 = await md5File(targetFilePath) |
| 98 | + expect(targetMd5).toEqual(sourceMd5) |
| 99 | + fs.promises.unlink(targetFilePath) |
| 100 | + expect(targetMd5).toBe(sourceMd5) |
| 101 | + }) |
| 102 | + |
| 103 | + it('should read file text', async () => { |
| 104 | + const sourceFilePath = path.join( |
| 105 | + __dirname, |
| 106 | + '..', |
| 107 | + '..', |
| 108 | + '..', |
| 109 | + 'website', |
| 110 | + 'public', |
| 111 | + 'logo.png', |
| 112 | + ) |
| 113 | + |
| 114 | + const formData = new FormData() |
| 115 | + formData.set( |
| 116 | + 'operations', |
| 117 | + JSON.stringify({ |
| 118 | + query: /* GraphQL */ ` |
| 119 | + mutation readTextFile($file: File!) { |
| 120 | + readTextFile(file: $file) |
| 121 | + } |
| 122 | + `, |
| 123 | + }), |
| 124 | + ) |
| 125 | + formData.set('map', JSON.stringify({ 0: ['variables.file'] })) |
| 126 | + formData.set( |
| 127 | + '0', |
| 128 | + new File( |
| 129 | + [await fs.promises.readFile(sourceFilePath)], |
| 130 | + path.basename(sourceFilePath), |
| 131 | + { type: 'image/png' }, |
| 132 | + ), |
| 133 | + ) |
| 134 | + |
| 135 | + const response = await fetch(`http://localhost:${port}/graphql`, { |
| 136 | + method: 'POST', |
| 137 | + body: formData, |
| 138 | + }) |
| 139 | + |
| 140 | + const body = await response.json() |
| 141 | + expect(body.errors).toBeUndefined() |
| 142 | + expect(body.data).toBeDefined() |
| 143 | + }) |
| 144 | +}) |
0 commit comments