Skip to content

Commit

Permalink
Add getRawPathComponent API to UriTemplate
Browse files Browse the repository at this point in the history
  • Loading branch information
ChaimaaeROUAI committed Dec 31, 2024
1 parent 5b17a13 commit b736048
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
13 changes: 12 additions & 1 deletion http/src/main/java/io/micronaut/http/uri/UriTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,18 @@ public int getRawSegmentLength() {
.reduce(Integer::sum)
.orElse(0);
}

/**
* Gets the raw unencoded path component from a URI.
*
* @param uri The URI string
* @return The raw path component, preserving encoding of special characters
*/
public static String getRawPathComponent(String uri) {
int queryIndex = uri.indexOf('?');
String pathOnly = queryIndex >= 0 ? uri.substring(0, queryIndex) : uri;
int lastSlash = pathOnly.lastIndexOf('/');
return lastSlash >= 0 ? pathOnly.substring(lastSlash + 1) : pathOnly;
}
/**
* Nests another URI template with this template.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.micronaut.http.uri;

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

class UriTemplateRawPathTest {
@Test
void testGetRawPathComponent() {
String uri = "/test/1.0/024-02-07T00:30:48.014+00:00";
assertEquals("024-02-07T00:30:48.014+00:00", UriTemplate.getRawPathComponent(uri));
}

@Test
void testGetRawPathComponentWithQuery() {
String uri = "/test/1.0/024-02-07T00:30:48.014+00:00?param=value";
assertEquals("024-02-07T00:30:48.014+00:00", UriTemplate.getRawPathComponent(uri));
}
}

0 comments on commit b736048

Please sign in to comment.