Skip to content

Commit

Permalink
feat(gatsby-plugin-sitemap): sanitize siteUrl (#12613)
Browse files Browse the repository at this point in the history
## Description

For sitemap plugin
Add code to remove trailing slash of siteUrl.

## Related Issues

Fix #12590
  • Loading branch information
rnitta authored and pieh committed Mar 16, 2019
1 parent 2a50218 commit 41bd265
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
81 changes: 81 additions & 0 deletions packages/gatsby-plugin-sitemap/src/__tests__/internals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
const {
runQuery,
defaultOptions: { serialize },
} = require(`../internals`)

describe(`results using default settings`, () => {
const generateQueryResultsMock = (
{ siteUrl } = { siteUrl: `http://dummy.url` }
) => {
return {
data: {
site: {
siteMetadata: {
siteUrl: siteUrl,
},
},
allSitePage: {
edges: [
{
node: {
path: `/page-1`,
},
},
{
node: {
path: `/page-2`,
},
},
],
},
},
}
}

const verifyUrlsExistInResults = (results, urls) => {
expect(results.map(result => result.url)).toEqual(urls)
}

const runTests = (pathPrefix = ``) => {
it(`prepares all urls correctly`, async () => {
const graphql = () => Promise.resolve(generateQueryResultsMock())
const queryRecords = await runQuery(graphql, ``, [], pathPrefix)
const urls = serialize(queryRecords)

verifyUrlsExistInResults(urls, [
`http://dummy.url${pathPrefix}/page-1`,
`http://dummy.url${pathPrefix}/page-2`,
])
})

it(`sanitize siteUrl`, async () => {
const graphql = () =>
Promise.resolve(
generateQueryResultsMock({ siteUrl: `http://dummy.url/` })
)
const queryRecords = await runQuery(graphql, ``, [], pathPrefix)
const urls = serialize(queryRecords)

verifyUrlsExistInResults(urls, [
`http://dummy.url${pathPrefix}/page-1`,
`http://dummy.url${pathPrefix}/page-2`,
])
})

it(`excludes pages`, async () => {
const graphql = () => Promise.resolve(generateQueryResultsMock())
const queryRecords = await runQuery(graphql, ``, [`/page-2`], pathPrefix)
const urls = serialize(queryRecords)

verifyUrlsExistInResults(urls, [`http://dummy.url${pathPrefix}/page-1`])
})
}

describe(`no path-prefix`, () => {
runTests()
})

describe(`with path-prefix`, () => {
runTests(`/path-prefix`)
})
})
7 changes: 7 additions & 0 deletions packages/gatsby-plugin-sitemap/src/internals.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ export const runQuery = (handler, query, excludes, pathPrefix) =>
return page
})

if (r.data.site.siteMetadata.siteUrl) {
// remove trailing slash of siteUrl
r.data.site.siteMetadata.siteUrl = withoutTrailingSlash(
r.data.site.siteMetadata.siteUrl
)
}

return r.data
})

Expand Down

0 comments on commit 41bd265

Please sign in to comment.