Skip to content

Commit

Permalink
Merge branch 'main' into rosado/571-content-and-styling-1
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeGoodall-GovUk authored Nov 7, 2024
2 parents a1b23fd + 0fcc050 commit b67e606
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/controllers/submitUrlController.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import axios from 'axios'
import { allowedFileTypes } from '../utils/utils.js'
import config from '../../config/index.js'

const HTTP_STATUS_METHOD_NOT_ALLOWED = 405

class SubmitUrlController extends UploadController {
async post (req, res, next) {
const localValidationErrorType = await SubmitUrlController.localUrlValidation(req.body.url)
Expand Down Expand Up @@ -63,6 +65,21 @@ class SubmitUrlController extends UploadController {
])
const headResponse = await SubmitUrlController.headRequest(url)

if (!headResponse) {
logger.warn('submitUrlController/localUrlValidation: failed to get the submitted urls head, skipping post validators', {
type: types.DataFetch
})
return null
}

if (headResponse?.status === HTTP_STATUS_METHOD_NOT_ALLOWED) {
// HEAD request not allowed, return null or a specific error message
logger.warn('submitUrlController/localUrlValidation: failed to get the submitted urls head as it was not allowed (405) skipping post validators', {
type: types.DataFetch
})
return null
}

return postValidators(headResponse).find(validator => !validator.fn())?.type
}

Expand Down
12 changes: 12 additions & 0 deletions test/unit/submitUrlController.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,18 @@ describe('SubmitUrlController', async () => {
url += 'a'.repeat(2048)
expect(await SubmitUrlController.localUrlValidation(url)).toBe('length')
})

it('should return null if the head request fails', async () => {
mocks.headMock.mockImplementation(() => { throw new Error('Head request failed') })
const url = 'http://example.com'
expect(await SubmitUrlController.localUrlValidation(url)).toBeNull()
})

it('should return null if the head request method is not allowed', async () => {
mocks.headMock.mockImplementation(() => ({ status: 405 }))
const url = 'http://example.com'
expect(await SubmitUrlController.localUrlValidation(url)).toBeNull()
})
})

describe('urlIsValid', () => {
Expand Down

0 comments on commit b67e606

Please sign in to comment.