Skip to content

Commit

Permalink
Add tests for handling mwApiPath and mwWikiPath params in base url di…
Browse files Browse the repository at this point in the history
…rector
  • Loading branch information
VadimKovalenkoSNF committed Oct 31, 2023
1 parent d592caf commit 80981bd
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
4 changes: 0 additions & 4 deletions src/MediaWiki.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,6 @@ class MediaWiki {
this.wikimediaMobileUrlDirector = new WikimediaMobileURLDirector(this.WikimediaMobileApiUrl.href)
}

/**
* What if wikiPath and apiPath set a bit earlier?
* No need to initialize this at the beginning of singleton
*/
private initApiURLDirector() {
this.webUrl = this.baseUrlDirector.buildURL(this.#wikiPath)
this.apiUrl = this.baseUrlDirector.buildURL(this.#apiPath, this.#wikiPath)
Expand Down
3 changes: 3 additions & 0 deletions src/util/builders/url/base.director.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ export default class BaseURLDirector {
buildURL(path: string, wikiPath = '') {
path = this.stripLeadingSlash(path)
wikiPath = this.stripLeadingSlash(wikiPath)
if (wikiPath && !wikiPath.endsWith('/')) {
wikiPath += '/'
}
path = `${wikiPath}${path}`
return urlBuilder.setDomain(this.baseDomain).setPath(path).build(true)
}
Expand Down
18 changes: 18 additions & 0 deletions test/unit/builders/url/base.director.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,24 @@ describe('BaseURLDirector', () => {

expect(url.href).toBe('https://en.m.wikipedia.com/v1/test/api')
})

it('should return URL object with mwApiPath param', () => {
const url = baseUrlDirector.buildURL('api.php')

expect(url.href).toBe('https://en.m.wikipedia.com/api.php')
})

it('should return URL object with mwApiPath ans mwWikiPath params', () => {
const url = baseUrlDirector.buildURL('api.php', 'w')

expect(url.href).toBe('https://en.m.wikipedia.com/w/api.php')
})

it('forward slashes at the beginnig of mwApiPath ans mwWikiPath params should be trimmed', () => {
const url = baseUrlDirector.buildURL('/api.php', '/w')

expect(url.href).toBe('https://en.m.wikipedia.com/w/api.php')
})
})

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

0 comments on commit 80981bd

Please sign in to comment.