fix(redirect): don't have to make relative url to absolute one (#541)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* fix(redirect): don't have to make relative url to absolute one * remove `isAbsoluteURL`
authored by Yusuke Wada · committed by GitHub · parent a2cc1a0 · Browse files · 7 files·+3 added−54 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
7 files+3 added−54 removed
- tests:+3 added−22 removed
- other:+0 added−32 removed
Provenance
- Commit
730453a- Tree
3dbfc81- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
a2cc1a0
7 files changed+3 added−54 removed
modified:deno_dist/utils/url.ts-10 removed
12123448 unmodified lines5354555659606162636465665758596027 unmodified linesconst URL_REGEXP = /^https?:\/\/[a-zA-Z0-9\-\.:]+(\/?[^?#]*)/
export type Pattern = readonly [string, string, RegExp | true] | '*'
export const splitPath = (path: string): string[] => { const paths = path.split(/\//) // faster than path.split('/')48 unmodified lines const result = queryIndex !== -1 ? url.substring(queryIndex) : '' return result}
export const isAbsoluteURL = (url: string): boolean => { const match = url.match(URL_REGEXP) if (match) { return true } return false}
export const mergePath = (...paths: string[]): string => { let p: string = '' let endsWithSlash = false
27 unmodified linesmodified:deno_dist/context.ts-6 removed
7 unmodified lines8 import { defaultNotFoundMessage } from './hono.ts'9 import type { CookieOptions } from './utils/cookie.ts'10 import { serialize } from './utils/cookie.ts'11 import type { StatusCode } from './utils/http-status.ts'12-import { isAbsoluteURL } from './utils/url.ts'12 13 type Headers = Record<string, string>14 export type Data = string | ArrayBuffer | ReadableStream15 161 unmodified lines177 return this.body(html, status, headers)178 }179 180 redirect(location: string, status: StatusCode = 302): Response {182- if (!isAbsoluteURL(location)) {183- const url = new URL(this.req.url)184- url.pathname = location185- location = url.toString()186- }181 return this.newResponse(null, status, {182 Location: location,183 })184 }10 unmodified linesmodified:src/utils/url.test.ts+1 added-20 removed
1-import {2- splitPath,3- getPattern,4- getPathFromURL,5- isAbsoluteURL,6- mergePath,7- getQueryStringFromURL,8-} from './url'1+import { splitPath, getPattern, getPathFromURL, mergePath, getQueryStringFromURL } from './url'2 3 describe('url', () => {4 it('splitPath', () => {5 let ps = splitPath('/')71 unmodified lines77 expect(queryString).toBe('?foo=bar?foo2=bar2')78 })79 })80 88- describe('isAbsoluteURL', () => {89- it('absolute url', () => {90- expect(isAbsoluteURL('https://example.com')).toBeTruthy()91- expect(isAbsoluteURL('https://example.com/xxx')).toBeTruthy()92- })93- it('relative url', () => {94- expect(isAbsoluteURL('/')).not.toBeTruthy()95- expect(isAbsoluteURL('/location')).not.toBeTruthy()96- expect(isAbsoluteURL('')).not.toBeTruthy()97- })98- })99-81 describe('mergePath', () => {82 it('mergePath', () => {83 expect(mergePath('/book', '/')).toBe('/book')84 expect(mergePath('/book/', '/')).toBe('/book/')25 unmodified linesmodified:src/utils/url.ts-10 removed
12123448 unmodified lines5354555659606162636465665758596027 unmodified linesconst URL_REGEXP = /^https?:\/\/[a-zA-Z0-9\-\.:]+(\/?[^?#]*)/
export type Pattern = readonly [string, string, RegExp | true] | '*'
export const splitPath = (path: string): string[] => { const paths = path.split(/\//) // faster than path.split('/')48 unmodified lines const result = queryIndex !== -1 ? url.substring(queryIndex) : '' return result}
export const isAbsoluteURL = (url: string): boolean => { const match = url.match(URL_REGEXP) if (match) { return true } return false}
export const mergePath = (...paths: string[]): string => { let p: string = '' let endsWithSlash = false
27 unmodified linesmodified:src/context.test.ts+1 added-1 removed
53 unmodified lines54 55 it('c.redirect()', async () => {56 let res = c.redirect('/destination')57 expect(res.status).toBe(302)58- expect(res.headers.get('Location')).toMatch(/^https?:\/\/.+\/destination$/)58+ expect(res.headers.get('Location')).toBe('/destination')59 res = c.redirect('https://example.com/destination')60 expect(res.status).toBe(302)61 expect(res.headers.get('Location')).toBe('https://example.com/destination')62 })104 unmodified linesmodified:src/context.ts-6 removed
7 unmodified lines8 import { defaultNotFoundMessage } from './hono'9 import type { CookieOptions } from './utils/cookie'10 import { serialize } from './utils/cookie'11 import type { StatusCode } from './utils/http-status'12-import { isAbsoluteURL } from './utils/url'12 13 type Headers = Record<string, string>14 export type Data = string | ArrayBuffer | ReadableStream15 161 unmodified lines177 return this.body(html, status, headers)178 }179 180 redirect(location: string, status: StatusCode = 302): Response {182- if (!isAbsoluteURL(location)) {183- const url = new URL(this.req.url)184- url.pathname = location185- location = url.toString()186- }181 return this.newResponse(null, status, {182 Location: location,183 })184 }10 unmodified linesmodified:src/hono.test.ts+1 added-1 removed
524 unmodified lines525 526 it('Absolute URL', async () => {527 const res = await app.request('https://example.com/redirect')528 expect(res.status).toBe(302)529- expect(res.headers.get('Location')).toBe('https://example.com/')529+ expect(res.headers.get('Location')).toBe('/')530 })531 })532 533 describe('Error handle', () => {569 unmodified lines