Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not add default query if it exists in the original URL #733

Merged
merged 4 commits into from
Feb 21, 2024

Conversation

crazytonyli
Copy link
Contributor

@crazytonyli crazytonyli commented Feb 21, 2024

Description

While updating unit tests, I have caught an bug in the exiting implementation of appending default 'locale' query to all WP.com REST API call. The expected behaviour is in testThatLocaleIsNotAppendedIfAlreadyIncludedInPath.

Details

WordPressComRestApi adds a locale=en (or whatever the device locale is) query to all HTTP requests, unless the 'local' is passed as parameter. For example:

  1. WordPressComRestApi.get("/post/1") should send an HTTP request with the default 'locale=en' in the URL: https://.../post/1?locale=en.
  2. WordPressComRestApi.get("/post/1", parameters: ["locale": "zh"]) should send an HTTP request with locale=zh, instead of the default query "locale=en".

However, there is a case missed in the current implementation, WordPressComRestApi.get("/post/1?locale=zh") should behave like the second example above and send an HTTP request with locale=zh.

Testing Details

The bug fix is covered in the existing testThatLocaleIsNotAppendedIfAlreadyIncludedInPath and I have added a new dedicate unit test for it.


  • Please check here if your pull request includes additional test coverage.
  • I have considered if this change warrants release notes and have added them to the appropriate section in the CHANGELOG.md if necessary.

// Given
let path = "/path/path"
let preferredLanguageIdentifier = WordPressComLanguageDatabase().deviceLanguage.slug
func testThatAppendingLocaleWorks() async throws {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think of

Suggested change
func testThatAppendingLocaleWorks() async throws {
func testAddsLocaleToURLQueryByDefault() async throws {

}

func testThatAppendingLocaleWorksWithExistingParams() {
// Given
func testThatAppendingLocaleWorksWithExistingParams() async throws {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think of

Suggested change
func testThatAppendingLocaleWorksWithExistingParams() async throws {
func testAddsLocaleToURLQueryByDefaultAndMaintainsInputParameters() async throws {

@@ -205,6 +205,15 @@ class HTTPRequestBuilderTests: XCTestCase {
try XCTAssertEqual(builder.query(name: "foo", value: "bar").build().url?.query, "locale=zh&foo=bar")
}

func testDefaultQueryExistsInOriginalURL() throws {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think of

Suggested change
func testDefaultQueryExistsInOriginalURL() throws {
func testDoesNotOverriedQueryItemInOriginalURL() throws {

@crazytonyli crazytonyli merged commit 4df2b39 into trunk Feb 21, 2024
7 checks passed
@crazytonyli crazytonyli deleted the fix-locale-in-path branch February 21, 2024 22:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants