From ed9f4aefaba22a65c49b3ba162c9a3c864043f32 Mon Sep 17 00:00:00 2001 From: Tilman Vatteroth Date: Wed, 9 Jun 2021 21:54:12 +0200 Subject: [PATCH] Add mock for upload response Signed-off-by: Tilman Vatteroth --- cypress/integration/fileUpload.spec.ts | 4 ++-- public/mock-backend/api/private/media/upload-post | 3 +++ src/api/media/index.ts | 13 +++++++------ .../editor-page/editor-pane/upload-handler.ts | 5 ++--- 4 files changed, 14 insertions(+), 11 deletions(-) create mode 100644 public/mock-backend/api/private/media/upload-post diff --git a/cypress/integration/fileUpload.spec.ts b/cypress/integration/fileUpload.spec.ts index 03eb4d625..9bab72c2c 100644 --- a/cypress/integration/fileUpload.spec.ts +++ b/cypress/integration/fileUpload.spec.ts @@ -31,7 +31,7 @@ describe('File upload', () => { beforeEach(() => { cy.intercept({ method: 'POST', - url: '/mock-backend/api/private/media/upload' + url: '/mock-backend/api/private/media/upload-post' }, { statusCode: 201, body: { @@ -86,7 +86,7 @@ describe('File upload', () => { it('upload fails', () => { cy.intercept({ method: 'POST', - url: '/mock-backend/api/private/media/upload' + url: '/mock-backend/api/private/media/upload-post' }, { statusCode: 400 }) diff --git a/public/mock-backend/api/private/media/upload-post b/public/mock-backend/api/private/media/upload-post new file mode 100644 index 000000000..3aaa05f41 --- /dev/null +++ b/public/mock-backend/api/private/media/upload-post @@ -0,0 +1,3 @@ +{ + "link": "/mock-backend/public/img/avatar.png" +} diff --git a/src/api/media/index.ts b/src/api/media/index.ts index 1b8e29306..69db3d201 100644 --- a/src/api/media/index.ts +++ b/src/api/media/index.ts @@ -5,6 +5,7 @@ */ import { ImageProxyResponse } from '../../components/markdown-renderer/replace-components/image/types' +import { isMockMode } from '../../utils/test-modes' import { defaultFetchConfig, expectResponseCode, getApiUrl } from '../utils' export const getProxiedUrl = async (imageUrl: string): Promise => { @@ -23,16 +24,16 @@ export interface UploadedMedia { link: string } -export const uploadFile = async (noteId: string, contentType: string, media: Blob): Promise => { - const response = await fetch(getApiUrl() + 'media/upload', { +export const uploadFile = async (noteId: string, media: Blob): Promise => { + const response = await fetch(`${getApiUrl()}media/upload${isMockMode() ? '-post' : ''}`, { ...defaultFetchConfig, headers: { - 'Content-Type': contentType, + 'Content-Type': media.type, 'HedgeDoc-Note': noteId }, - method: 'POST', - body: media + method: isMockMode() ? 'GET' : 'POST', + body: isMockMode() ? undefined : media }) - expectResponseCode(response, 201) + expectResponseCode(response, isMockMode() ? 200 : 201) return (await response.json()) as Promise } diff --git a/src/components/editor-page/editor-pane/upload-handler.ts b/src/components/editor-page/editor-pane/upload-handler.ts index f74d0e046..b379c254d 100644 --- a/src/components/editor-page/editor-pane/upload-handler.ts +++ b/src/components/editor-page/editor-pane/upload-handler.ts @@ -14,8 +14,7 @@ export const handleUpload = (file: File, editor: Editor): void => { if (!file) { return } - const mimeType = file.type - if (!supportedMimeTypes.includes(mimeType)) { + if (!supportedMimeTypes.includes(file.type)) { // this mimetype is not supported return } @@ -23,7 +22,7 @@ export const handleUpload = (file: File, editor: Editor): void => { const uploadPlaceholder = `![${i18n.t('editor.upload.uploadFile', { fileName: file.name })}]()` const noteId = store.getState().noteDetails.id editor.replaceRange(uploadPlaceholder, cursor, cursor, '+input') - uploadFile(noteId, mimeType, file) + uploadFile(noteId, file) .then(({ link }) => { editor.replaceRange( getCorrectSyntaxForLink(mimeType, link),