Skip to content

Conversation

@hjaret
Copy link
Contributor

@hjaret hjaret commented Oct 9, 2024

  1. Handle case where beginQuery is called even though the given URI contained a query
  2. Handle other cases where methods are called out of correct order

Comment on lines 104 to 109
try {
test("http://acme.com#frag", b -> b.withSegment("bar").with("foo", "bar"), "http://acme.com/bar#frag");
fail("Expected this to fail");
} catch (Exception e) {
assertEquals(e.getMessage(), "You cannot add a URL segment after you have appended a # to the end of the URL");
}
Copy link
Member

Choose a reason for hiding this comment

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

Ideally you'd add another method to this test harness to do this work. This will be cleaner and make it easier to add additional failure case tests in the future.

For example:

// You cannot add a segment after a fragment. 
testInvalid("http://acme.com#frag", b -> b.withSegment("bar").with("foo", "bar"),
            "You cannot add a URL segment after you have appended a # to the end of the URL");
private void testInvalid(String uri, Consumer<QueryStringBuilder> consumer, String expectedMessage) {
  try {
    QueryStringBuilder builder = QueryStringBuilder.builder(uri);
    consumer.accept(builder);
    fail("Expected this to fail.");
  } catch (Exception e) {
    assertEquals(e.getMessage(), expectedMessage);
  }
}                   

Comment on lines 52 to 63
// If query string contains no terms, remove the question mark
if (sb.toString().endsWith("?")) {
sb.setLength(sb.length() - 1);
}

if (sb.indexOf("#") == -1) {
sb.append("#");
addSeparator = false;
} else {
char lastChar = sb.charAt(sb.length() - 1);
addSeparator = lastChar != '#' && lastChar != '&';
}
Copy link
Member

Choose a reason for hiding this comment

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

I don't know that this is correct. I believe you can add a # after a ? in a URL.

For example, I believe this is a valid URL: https://example.com?foo=bar#section-1

Copy link
Member

Choose a reason for hiding this comment

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

Per our conversation, I think I misunderstood what this was doing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

https://example.com?foo=bar#section-1 is definitely valid. I was just trying to avoid
https://example.com?#section-1 but I can certainly allow it.

Copy link
Member

@robotdan robotdan left a comment

Choose a reason for hiding this comment

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

Looks like we have pretty good test coverage. But if you can make sure we have a test for any new behavior added that would be great!

Comment on lines 52 to 63
// If query string contains no terms, remove the question mark
if (sb.toString().endsWith("?")) {
sb.setLength(sb.length() - 1);
}

if (sb.indexOf("#") == -1) {
sb.append("#");
addSeparator = false;
} else {
char lastChar = sb.charAt(sb.length() - 1);
addSeparator = lastChar != '#' && lastChar != '&';
}
Copy link
Member

Choose a reason for hiding this comment

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

Per our conversation, I think I misunderstood what this was doing.

Comment on lines 52 to 55
// If query string contains no terms, remove the question mark
if (sb.toString().endsWith("?")) {
sb.setLength(sb.length() - 1);
}
Copy link
Member

Choose a reason for hiding this comment

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

I don't think it is illegal to have an empty query of ? so I would leave this. I'd rather not enforce anything here unless it is truly illegal.

Comment on lines +58 to +59
sb.append("#");
addSeparator = false;
Copy link
Member

Choose a reason for hiding this comment

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

We should just double check that the URL spec never allows more than one #. Seems likely but I don't know for sure.

From my Googling it does appear to be a safe assumption.

https://www.webdevsplanet.com/post/url-parameters-vs-fragments

A URL cannot have more than one fragment.

Comment on lines 194 to 198
if (!sb.isEmpty() && (sb.indexOf("?") != -1)) {
throw new IllegalStateException(String.format(message, "?"));
}
if (!sb.isEmpty() && (sb.indexOf("#") != -1)) {
throw new IllegalStateException(String.format(message, "#"));
Copy link
Member

Choose a reason for hiding this comment

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

Should we combine the sb.isEmpty check?

    String message = "You cannot add a URL segment after you have appended a %s to the end of the URL";
    if (!sb.isEmpty()) {
      if (sb.indexOf("?") != -1) {
        throw new IllegalStateException(String.format(message, "?"));
      }
      
      if (sb.indexOf("#") != -1) {
        throw new IllegalStateException(String.format(message, "#"));
      }  
    }

Copy link
Contributor

@lyleschemmerling lyleschemmerling left a comment

Choose a reason for hiding this comment

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

lgtm

@hjaret hjaret merged commit 24c1b74 into master Oct 11, 2024
2 checks passed
@hjaret hjaret deleted the hjaret/query-builder branch October 11, 2024 13:23
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.

4 participants