|
23 | 23 |
|
24 | 24 | import feign.Request.HttpMethod; |
25 | 25 | import feign.template.UriUtils; |
| 26 | +import java.net.URI; |
26 | 27 | import java.util.Arrays; |
27 | 28 | import java.util.Collection; |
28 | 29 | import java.util.Collections; |
@@ -579,6 +580,114 @@ void fragmentShouldBeExtractedWhenQueryParamsExist() { |
579 | 580 | assertThat(template.queryLine()).isEqualTo("?query=queryValue"); |
580 | 581 | } |
581 | 582 |
|
| 583 | + @Test |
| 584 | + void resolvingUriShouldPreserveFragment() { |
| 585 | + RequestTemplate template = |
| 586 | + new RequestTemplate().method(HttpMethod.GET).uri("/path/{id}#anchor"); |
| 587 | + |
| 588 | + RequestTemplate resolved = template.resolve(Collections.singletonMap("id", "123")); |
| 589 | + |
| 590 | + assertThat(resolved.url()).isEqualTo("/path/123#anchor"); |
| 591 | + assertThat(resolved.request().url()).isEqualTo("/path/123#anchor"); |
| 592 | + assertThat(template.url()).isEqualTo("/path/{id}#anchor"); |
| 593 | + } |
| 594 | + |
| 595 | + @Test |
| 596 | + void replacingUriShouldClearFragmentFromPreviousUri() { |
| 597 | + RequestTemplate template = new RequestTemplate().method(HttpMethod.GET).uri("/old#stale"); |
| 598 | + assertThat(template.url()).isEqualTo("/old#stale"); |
| 599 | + |
| 600 | + /* cannot set a fragment on the replacement uri, the previous one must not survive */ |
| 601 | + template.uri("/new?q=v"); |
| 602 | + |
| 603 | + assertThat(template.url()).isEqualTo("/new?q=v"); |
| 604 | + assertThat(template.queryLine()).isEqualTo("?q=v"); |
| 605 | + } |
| 606 | + |
| 607 | + @Test |
| 608 | + void explicitlyEmptyFragmentShouldReplacePreviousFragment() { |
| 609 | + RequestTemplate template = new RequestTemplate().method(HttpMethod.GET).uri("/old#stale"); |
| 610 | + |
| 611 | + /* a bare '#' replaces the stale fragment with an empty one */ |
| 612 | + template.uri("/new#"); |
| 613 | + |
| 614 | + /* |
| 615 | + * RFC 3986 allows an empty fragment and java.net.URI reports it as "" rather than null, so an |
| 616 | + * empty fragment is not asserted as stripped, only as no longer being the stale one. |
| 617 | + */ |
| 618 | + String url = template.url(); |
| 619 | + assertThat(url).doesNotContain("stale"); |
| 620 | + assertThat(url).isEqualTo("/new#"); |
| 621 | + assertThat(URI.create(url).getRawFragment()).isNotNull().isEmpty(); |
| 622 | + } |
| 623 | + |
| 624 | + @Test |
| 625 | + void appendingUriShouldPreserveExistingFragment() { |
| 626 | + RequestTemplate template = new RequestTemplate().method(HttpMethod.GET).uri("/base#anchor"); |
| 627 | + |
| 628 | + template.uri("/suffix", true); |
| 629 | + |
| 630 | + assertThat(template.url()).isEqualTo("/base/suffix#anchor"); |
| 631 | + } |
| 632 | + |
| 633 | + @Test |
| 634 | + void incomingFragmentShouldReplaceExistingFragment() { |
| 635 | + RequestTemplate replaced = |
| 636 | + new RequestTemplate().method(HttpMethod.GET).uri("/old#stale").uri("/new#fresh"); |
| 637 | + assertThat(replaced.url()).isEqualTo("/new#fresh"); |
| 638 | + |
| 639 | + RequestTemplate appended = |
| 640 | + new RequestTemplate().method(HttpMethod.GET).uri("/base#stale").uri("/suffix#fresh", true); |
| 641 | + assertThat(appended.url()).isEqualTo("/base/suffix#fresh"); |
| 642 | + } |
| 643 | + |
| 644 | + @Test |
| 645 | + void targetShouldPreserveRawFragment() { |
| 646 | + RequestTemplate template = |
| 647 | + new RequestTemplate() |
| 648 | + .method(HttpMethod.GET) |
| 649 | + .target("https://example.com/a#encoded%20value%2F"); |
| 650 | + |
| 651 | + /* the raw fragment is re-appended verbatim, not decoded into " " or "/" */ |
| 652 | + assertThat(template.url()).isEqualTo("https://example.com/a#encoded%20value%2F"); |
| 653 | + assertThat(template.headers()).isEmpty(); |
| 654 | + assertThat(template.queries()).isEmpty(); |
| 655 | + } |
| 656 | + |
| 657 | + @Test |
| 658 | + void targetShouldPreserveRawFragmentWhenQueryIsPresent() { |
| 659 | + RequestTemplate template = |
| 660 | + new RequestTemplate() |
| 661 | + .method(HttpMethod.GET) |
| 662 | + .target("https://example.com/a?x=1#encoded%20fragment"); |
| 663 | + |
| 664 | + assertThat(template.url()).isEqualTo("https://example.com/a?x=1#encoded%20fragment"); |
| 665 | + assertThat(template.queries()).containsKey("x"); |
| 666 | + } |
| 667 | + |
| 668 | + @Test |
| 669 | + void targetWithoutFragmentShouldNotClearExistingFragment() { |
| 670 | + RequestTemplate template = |
| 671 | + new RequestTemplate() |
| 672 | + .method(HttpMethod.GET) |
| 673 | + .uri("/path#fragment") |
| 674 | + .target("https://example.com"); |
| 675 | + |
| 676 | + assertThat(template.url()).isEqualTo("https://example.com/path#fragment"); |
| 677 | + } |
| 678 | + |
| 679 | + @Test |
| 680 | + void copyShouldPreserveAndIsolateFragment() { |
| 681 | + RequestTemplate original = new RequestTemplate().method(HttpMethod.GET).uri("/path#fragment"); |
| 682 | + |
| 683 | + RequestTemplate copy = RequestTemplate.from(original); |
| 684 | + assertThat(copy.url()).isEqualTo("/path#fragment"); |
| 685 | + |
| 686 | + copy.uri("/other#other-fragment"); |
| 687 | + assertThat(copy.url()).isEqualTo("/other#other-fragment"); |
| 688 | + assertThat(original.url()).isEqualTo("/path#fragment"); |
| 689 | + } |
| 690 | + |
582 | 691 | @Test |
583 | 692 | void urlEncodingRemainsInPlace() { |
584 | 693 | RequestTemplate template = |
|
0 commit comments