Skip to content
This repository has been archived by the owner on Feb 29, 2020. It is now read-only.

Commit

Permalink
[client][managed][offline] Allow relative URI in read and invokeapi
Browse files Browse the repository at this point in the history
  • Loading branch information
hasankhan committed Oct 9, 2014
1 parent 53bc24f commit 5b3c6b3
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ private async Task<string> InternalInvokeApiAsync(string apiName, string content
/// <returns></returns>
private string CreateAPIUriString(string apiName, IDictionary<string, string> parameters = null)
{
string uriFragment = string.Format(CultureInfo.InvariantCulture, "api/{0}", apiName);
string uriFragment = apiName.StartsWith("/") ? apiName : string.Format(CultureInfo.InvariantCulture, "api/{0}", apiName);
string queryString = MobileServiceUrlBuilder.GetQueryString(parameters, useTableAPIRules: false);

return MobileServiceUrlBuilder.CombinePathAndQuery(uriFragment, queryString);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public Task<IEnumerable<U>> ReadAsync<U>(IMobileServiceTableQuery<U> query)
[SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "Generic are not nested when used via async.")]
public async Task<IEnumerable<U>> ReadAsync<U>(string query)
{
QueryResult result = await base.ReadAsync(query, null, MobileServiceFeatures.TypedTable | MobileServiceFeatures.ReadWithLinkHeader);
QueryResult result = await base.ReadAsync(query, null, MobileServiceFeatures.TypedTable);

return new QueryResultEnumerable<U>(
result.TotalCount,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,12 @@ internal virtual async Task<QueryResult> ReadAsync(string query, IDictionary<str

string uriPath;
Uri uri;
if (Uri.TryCreate(query, UriKind.Absolute, out uri))
if (query.StartsWith("/") && Uri.TryCreate(this.MobileServiceClient.ApplicationUri, query, out uri))
{
uriPath = uri.GetComponents(UriComponents.Scheme | UriComponents.UserInfo | UriComponents.Host | UriComponents.Port | UriComponents.Path, UriFormat.UriEscaped);
query = uri.Query;
}
else if (Uri.TryCreate(query, UriKind.Absolute, out uri))
{
features |= MobileServiceFeatures.ReadWithLinkHeader;
uriPath = uri.GetComponents(UriComponents.Scheme | UriComponents.UserInfo | UriComponents.Host | UriComponents.Port | UriComponents.Path, UriFormat.UriEscaped);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,19 @@ public async Task InvokeCustomAPIGet()
Assert.AreEqual(3, expected.Id);
}

[AsyncTestMethod]
public async Task InvokeApiAsync_DoesNotAppendApiPath_IfApiStartsWithSlash()
{
TestHttpHandler hijack = new TestHttpHandler();
var service = new MobileServiceClient("http://www.test.com", "secret...", hijack);
hijack.SetResponseContent("{\"id\":3}");

await service.InvokeApiAsync<IntType>("/calculator/add?a=1&b=2", HttpMethod.Get, null);

Assert.AreEqual(hijack.Request.RequestUri.LocalPath, "/calculator/add");
Assert.Contains(hijack.Request.RequestUri.Query, "a=1&b=2");
}

[AsyncTestMethod]
public async Task InvokeCustomAPIGetJToken()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,40 @@ public async Task ReadAsync_WithAbsoluteUri_Generic()
Assert.AreEqual("http://wwww.contoso.com/about?$filter=a eq b&$orderby=c", hijack.Request.RequestUri.ToString());
}

[AsyncTestMethod]
public async Task ReadAsync_WithRelativeUri_Generic()
{
var data = new[]
{
new
{
ServiceUri = "http://www.test.com",
QueryUri = "/about?$filter=a eq b&$orderby=c",
RequestUri = "http://www.test.com/about?$filter=a eq b&$orderby=c"
},
new
{
ServiceUri = "http://www.test.com/",
QueryUri = "/about?$filter=a eq b&$orderby=c",
RequestUri = "http://www.test.com/about?$filter=a eq b&$orderby=c"
}
};

foreach (var item in data)
{
var hijack = new TestHttpHandler();
hijack.SetResponseContent("[{\"col1\":\"Hey\"}]");
IMobileServiceClient service = new MobileServiceClient(item.ServiceUri, "secret...", hijack);

IMobileServiceTable<ToDo> table = service.GetTable<ToDo>();

await table.ReadAsync<ToDo>(item.QueryUri);

Assert.AreEqual("TT", hijack.Request.Headers.GetValues("X-ZUMO-FEATURES").First());
Assert.AreEqual(item.RequestUri, hijack.Request.RequestUri.ToString());
}
}

[AsyncTestMethod]
public async Task ReadAsync_Returns_IQueryResultEnumerable()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,39 @@ public async Task ReadAsync_WithAbsoluteUri()
Assert.AreEqual("http://wwww.contoso.com/about/?$filter=a eq b&$orderby=c", hijack.Request.RequestUri.ToString());
}

[AsyncTestMethod]
public async Task ReadAsync_WithRelativeUri()
{
var data = new[]
{
new
{
ServiceUri = "http://www.test.com",
QueryUri = "/about?$filter=a eq b&$orderby=c",
RequestUri = "http://www.test.com/about?$filter=a eq b&$orderby=c"
},
new
{
ServiceUri = "http://www.test.com/",
QueryUri = "/about?$filter=a eq b&$orderby=c",
RequestUri = "http://www.test.com/about?$filter=a eq b&$orderby=c"
}
};

foreach (var item in data)
{
var hijack = new TestHttpHandler();
hijack.SetResponseContent("[{\"String\":\"Hey\"}]");
IMobileServiceClient service = new MobileServiceClient(item.ServiceUri, "secret...", hijack);

IMobileServiceTable table = service.GetTable("someTable");

await table.ReadAsync(item.QueryUri);

Assert.AreEqual(item.RequestUri, hijack.Request.RequestUri.ToString());
}
}

[AsyncTestMethod]
public async Task ReadAsyncWithStringIdFilter()
{
Expand Down

0 comments on commit 5b3c6b3

Please sign in to comment.