Skip to content
Myr

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

7 files+3 added−54 removed

modified:deno_dist/utils/url.ts-10 removed
modified:deno_dist/context.ts-6 removed
7 unmodified lines
8import { defaultNotFoundMessage } from './hono.ts'9import type { CookieOptions } from './utils/cookie.ts'10import { serialize } from './utils/cookie.ts'11import type { StatusCode } from './utils/http-status.ts'12import { isAbsoluteURL } from './utils/url.ts'1213type Headers = Record<string, string>14export type Data = string | ArrayBuffer | ReadableStream15
161 unmodified lines
177 return this.body(html, status, headers)178 }179180 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 lines
modified:src/utils/url.test.ts+1 added-20 removed
1import {2 splitPath,3 getPattern,4 getPathFromURL,5 isAbsoluteURL,6 mergePath,7 getQueryStringFromURL,8} from './url'1import { splitPath, getPattern, getPathFromURL, mergePath, getQueryStringFromURL } from './url'23describe('url', () => {4 it('splitPath', () => {5 let ps = splitPath('/')
71 unmodified lines
77 expect(queryString).toBe('?foo=bar?foo2=bar2')78 })79 })8088 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 })9981 describe('mergePath', () => {82 it('mergePath', () => {83 expect(mergePath('/book', '/')).toBe('/book')84 expect(mergePath('/book/', '/')).toBe('/book/')
25 unmodified lines
modified:src/utils/url.ts-10 removed
modified:src/context.test.ts+1 added-1 removed
53 unmodified lines
5455 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 lines
modified:src/context.ts-6 removed
7 unmodified lines
8import { defaultNotFoundMessage } from './hono'9import type { CookieOptions } from './utils/cookie'10import { serialize } from './utils/cookie'11import type { StatusCode } from './utils/http-status'12import { isAbsoluteURL } from './utils/url'1213type Headers = Record<string, string>14export type Data = string | ArrayBuffer | ReadableStream15
161 unmodified lines
177 return this.body(html, status, headers)178 }179180 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 lines
modified:src/hono.test.ts+1 added-1 removed
524 unmodified lines
525526 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})532533describe('Error handle', () => {
569 unmodified lines