Bugs in uriRemoveBaseUri
disigma opened this issue · comments
When I test with codes below
UriUriA base;
UriParserStateA state;
state.uri = &base;
ASSERT_EQ(uriParseUriA(&state, "http://example2/x/y/z"), URI_SUCCESS);
UriUriA source;
state.uri = &source;
ASSERT_EQ(uriParseUriA(&state, "http://example/x/abc"), URI_SUCCESS);
UriUriA dest;
ASSERT_EQ(uriRemoveBaseUriA(&dest, &source, &base, URI_FALSE), URI_SUCCESS);
int size = 0;
ASSERT_EQ(uriToStringCharsRequiredA(&dest, &size), URI_SUCCESS);
char buffer[size + 1];
ASSERT_EQ(uriToStringA(buffer, &dest, size + 1, &size), URI_SUCCESS);
ASSERT_STREQ(buffer, "//example/x/abc");
It fails
Failure
Expected: buffer
Which is: "../abc"
To be equal to: "//example/x/abc"
Hi @disigma, thanks for the report!
I get why you'd expect //example/x/abc
for an answer for these two inputs.
EDIT: Mistaken remainder of this reply removed by author.
PS: Here's a standalone version of the code above that I used.
To run:
# gcc -o issue19{,.c} -luriparser && ./issue19 ; echo $?
issue19.c:33: strcmp("../abc", "//example/x/abc") == 0 failed
1
The code:
#include <assert.h>
#include <stdio.h>
#include <uriparser/Uri.h>
#define ASSERT_EQ(a, b) \
assert(a == b)
#define ASSERT_STREQ(a, b) \
do { \
if (strcmp(a, b) != 0) { \
fprintf(stderr, "%s:%d: strcmp(\"%s\", \"%s\") == 0 failed\n", \
__FILE__, __LINE__, a, b); \
exit(1); \
} \
} while(0)
int main() {
UriUriA base;
UriParserStateA state;
state.uri = &base;
ASSERT_EQ(uriParseUriA(&state, "http://example2/x/y/z"), URI_SUCCESS);
UriUriA source;
state.uri = &source;
ASSERT_EQ(uriParseUriA(&state, "http://example/x/abc"), URI_SUCCESS);
UriUriA dest;
ASSERT_EQ(uriRemoveBaseUriA(&dest, &source, &base, URI_FALSE), URI_SUCCESS);
int size = 0;
ASSERT_EQ(uriToStringCharsRequiredA(&dest, &size), URI_SUCCESS);
char buffer[size + 1];
ASSERT_EQ(uriToStringA(buffer, &dest, size + 1, &size), URI_SUCCESS);
ASSERT_STREQ(buffer, "//example/x/abc");
return 0;
}
I think it's better to update the documentation of this function.
I tried the test case in js, and got the result below:
> var source = new URI("http://example/x/abc")
< undefined
> source.relativeTo("http://example2/x/y/z").toString()
< "//example/x/abc"
which is the same result from uriparser after #20
I think it's better to update the documentation of this function.
Went for that in 13255b6 now.
My earlier comment in here was mostly mistaken so I removed it now. I'll keep the old edits for transparency.