Skip to content

Commit

Permalink
APIKIT-2743: Wrong base URI returned in APIKit for OData response (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
JuanAller authored Aug 28, 2020
1 parent 4fff68f commit 86e9e63
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public ODataApiKitFormatter(OdataMetadataManager odataMetadataManager, String en
this.setSupportsAtom(true);
}

public String format(Format format) throws Exception {
public String format(Format format) {
if (Format.Default.equals(format)) {
format = Format.Atom;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@
*/
package org.mule.module.apikit.odata.processor;

import static java.lang.System.getProperty;
import static org.mule.module.apikit.api.UrlUtils.FULL_DOMAIN;
import static org.mule.runtime.core.api.util.StringUtils.isBlank;
import org.mule.extension.http.api.HttpRequestAttributes;
import org.mule.module.apikit.odata.AbstractRouterInterface;
import org.mule.module.apikit.odata.ODataPayload;
import org.mule.module.apikit.odata.context.OdataContext;
import org.mule.module.apikit.odata.formatter.ODataPayloadFormatter.Format;
import org.mule.module.apikit.odata.metadata.OdataMetadataManager;
import org.mule.runtime.core.api.event.CoreEvent;

import java.util.List;
import java.util.regex.Pattern;

Expand All @@ -40,13 +42,11 @@ protected String getProtocol(HttpRequestAttributes attributes) {
}

protected String getHost(HttpRequestAttributes attributes) {
String host = attributes.getLocalAddress();
return host;
}

protected String getUrl(HttpRequestAttributes attributes) {
String url = getProtocol(attributes) + "://" + getHost(attributes);
return url;
String cloudHubFullDomain = getProperty(FULL_DOMAIN);
if (!isBlank(cloudHubFullDomain)) {
return cloudHubFullDomain;
}
return attributes.getHeaders().get("host");
}

protected String getCompleteUrl(HttpRequestAttributes attributes) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/
package org.mule.module.apikit.odata.processor;

import org.mule.extension.http.api.HttpRequestAttributes;
import org.mule.module.apikit.odata.AbstractRouterInterface;
import org.mule.module.apikit.odata.ODataPayload;
import org.mule.module.apikit.odata.context.OdataContext;
Expand All @@ -18,6 +17,7 @@
import java.util.List;

public class ODataServiceDocumentProcessor extends ODataRequestProcessor {

private static final String FULL_DOMAIN = "fullDomain";

public ODataServiceDocumentProcessor(OdataContext odataContext) {
Expand All @@ -38,12 +38,4 @@ public ODataPayload process(CoreEvent event, AbstractRouterInterface router, Lis
}
}

@Override
protected String getHost(HttpRequestAttributes attributes) {
String cloudHubFullDomain = System.getProperty(FULL_DOMAIN);
if (cloudHubFullDomain != null) {
return cloudHubFullDomain;
}
return super.getHost(attributes);
}
}
11 changes: 1 addition & 10 deletions src/test/munit/service-document/service-document-test.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,7 @@
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">


<munit:config name="odata-service-document-test-suite">
<munit:parameterizations>
<munit:parameterization name="CLOUDHUB">
<munit:parameters>
<munit:parameter propertyName="fullDomain" value="ch-https-41-demo.au-s1.cloudhub.io"/>
</munit:parameters>
</munit:parameterization>
</munit:parameterizations>
</munit:config>
<munit:config name="odata-service-document-test-suite"/>

<munit:test name="service-document-json">
<munit:enable-flow-sources>
Expand Down Expand Up @@ -51,7 +43,6 @@
<munit-tools:assert-that expression="#[output application/java --- attributes.headers.'Content-type']" is="#[MunitTools::equalTo('application/xml')]"/>
<munit-tools:assert-that expression="#[output application/java --- attributes.statusCode]" is="#[MunitTools::equalTo(200)]"/>
<munit-tools:assert-that expression="#[output application/java --- payload.service.workspace.collection.title]" is="#[MunitTools::equalTo('orders')]"/>
<munit-tools:assert-that expression="#[output application/java --- payload.service.@base]" is="#[MunitTools::equalTo('http://ch-https-41-demo.au-s1.cloudhub.io/api/odata.svc')]"/>
</munit:validation>
</munit:test>

Expand Down
62 changes: 62 additions & 0 deletions src/test/munit/service-document/service-document-url-test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?xml version="1.0" encoding="UTF-8"?>

<mule
xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:munit="http://www.mulesoft.org/schema/mule/munit"
xmlns:munit-tools="http://www.mulesoft.org/schema/mule/munit-tools"
xmlns="http://www.mulesoft.org/schema/mule/core"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/munit http://www.mulesoft.org/schema/mule/munit/current/mule-munit.xsd
http://www.mulesoft.org/schema/mule/munit-tools http://www.mulesoft.org/schema/mule/munit-tools/current/mule-munit-tools.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">


<munit:config name="odata-service-document-url-test-suite">
<munit:parameterizations>
<munit:parameterization name="CLOUDHUB">
<munit:parameters>
<munit:parameter propertyName="fullDomain" value="ch-https-41-demo.au-s1.cloudhub.io"/>
</munit:parameters>
</munit:parameterization>
<munit:parameterization name="ON-PREM">
<munit:parameters>
<munit:parameter propertyName="fullDomain" value=""/>
</munit:parameters>
</munit:parameterization>
</munit:parameterizations>
</munit:config>

<munit:test name="service-document-xml-base-url-with-full-domain" ignore="#['${fullDomain}' == '']">
<munit:enable-flow-sources>
<munit:enable-flow-source value="service-document-server-main"/>
</munit:enable-flow-sources>
<munit:execution>
<http:request method="GET" config-ref="http-requester-simple" path="/api/odata.svc">
<http:headers>#[output application/java --- {"Accept" : "application/xml"}]</http:headers>
</http:request>
</munit:execution>
<munit:validation>
<munit-tools:assert-that expression="#[output application/java --- attributes.headers.'Content-type']" is="#[MunitTools::equalTo('application/xml')]"/>
<munit-tools:assert-that expression="#[output application/java --- attributes.statusCode]" is="#[MunitTools::equalTo(200)]"/>
<munit-tools:assert-that expression="#[output application/java --- payload.service.@base]" is="#[MunitTools::equalTo('http://ch-https-41-demo.au-s1.cloudhub.io/api/odata.svc')]"/>
</munit:validation>
</munit:test>

<munit:test name="service-document-xml-base-url-without-full-domain" ignore="#['${fullDomain}' == 'ch-https-41-demo.au-s1.cloudhub.io']">
<munit:enable-flow-sources>
<munit:enable-flow-source value="service-document-server-main"/>
</munit:enable-flow-sources>
<munit:execution>
<http:request method="GET" config-ref="http-requester-simple" path="/api/odata.svc">
<http:headers>#[output application/java --- {"Accept" : "application/xml"}]</http:headers>
</http:request>
</munit:execution>
<munit:validation>
<munit-tools:assert-that expression="#[output application/java --- attributes.headers.'Content-type']" is="#[MunitTools::equalTo('application/xml')]"/>
<munit-tools:assert-that expression="#[output application/java --- attributes.statusCode]" is="#[MunitTools::equalTo(200)]"/>
<munit-tools:assert-that expression="#[output application/java --- payload.service.@base]" is="#[MunitTools::startsWith('http://localhost:')]"/>
</munit:validation>
</munit:test>

</mule>
4 changes: 1 addition & 3 deletions src/test/munit/success-requests/success-requests-test.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
is="#[MunitTools::equalTo(10248)]"/>
<munit-tools:assert-that expression="#[output application/java --- payload.d.results.OrderID[1]]"
is="#[MunitTools::equalTo(10249)]"/>
<munit-tools:assert-that expression="#[output application/java --- payload.d.results[0].'__metadata'.uri]"
is="#[MunitTools::startsWith('http://127.0.0.1')]"/>
</munit:validation>
</munit:test>

Expand Down Expand Up @@ -234,4 +232,4 @@
</munit:validation>
</munit:test>

</mule>
</mule>
93 changes: 93 additions & 0 deletions src/test/munit/success-requests/success-requests-url-test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?xml version="1.0" encoding="UTF-8"?>

<mule
xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:munit="http://www.mulesoft.org/schema/mule/munit"
xmlns:munit-tools="http://www.mulesoft.org/schema/mule/munit-tools"
xmlns="http://www.mulesoft.org/schema/mule/core"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/munit http://www.mulesoft.org/schema/mule/munit/current/mule-munit.xsd
http://www.mulesoft.org/schema/mule/munit-tools http://www.mulesoft.org/schema/mule/munit-tools/current/mule-munit-tools.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">

<munit:config name="success-requests-url-test-suite">
<munit:parameterizations>
<munit:parameterization name="CLOUDHUB">
<munit:parameters>
<munit:parameter propertyName="fullDomain" value="ch-https-41-demo.au-s1.cloudhub.io"/>
</munit:parameters>
</munit:parameterization>
<munit:parameterization name="ON-PREM">
<munit:parameters>
<munit:parameter propertyName="fullDomain" value=""/>
</munit:parameters>
</munit:parameterization>
</munit:parameterizations>
</munit:config>


<munit:test name="get-entity-collection-results-metadata-uri-with-full-domain" ignore="#['${fullDomain}' == '']">
<munit:enable-flow-sources>
<munit:enable-flow-source value="success-server-main"/>
<munit:enable-flow-source value="get:\orders:success-server-config"/>
</munit:enable-flow-sources>
<munit:execution>
<http:request method="GET" config-ref="http-requester-simple" path="api/odata.svc/orders?$format=json">
</http:request>
</munit:execution>
<munit:validation>
<munit-tools:assert-that expression="#[output application/java --- payload.d.results[0].'__metadata'.uri]"
is="#[MunitTools::startsWith('http://ch-https-41-demo.au-s1.cloudhub.io')]"/>
</munit:validation>
</munit:test>

<munit:test name="get-entity-collection-results-metadata-uri-without-full-domain" ignore="#['${fullDomain}' == 'ch-https-41-demo.au-s1.cloudhub.io']">
<munit:enable-flow-sources>
<munit:enable-flow-source value="success-server-main"/>
<munit:enable-flow-source value="get:\orders:success-server-config"/>
</munit:enable-flow-sources>
<munit:execution>
<http:request method="GET" config-ref="http-requester-simple" path="api/odata.svc/orders?$format=json">
</http:request>
</munit:execution>
<munit:validation>
<munit-tools:assert-that expression="#[output application/java --- payload.d.results[0].'__metadata'.uri]"
is="#[MunitTools::startsWith('http://localhost')]"/>
</munit:validation>
</munit:test>


<munit:test name="get-entity-by-id-with-atom-format-uri-with-full-domain" ignore="#['${fullDomain}' == '']">
<munit:enable-flow-sources>
<munit:enable-flow-source value="success-server-main"/>
<munit:enable-flow-source value="get:\orders\(orderId):success-server-config"/>
</munit:enable-flow-sources>
<munit:execution>
<http:request method="GET" config-ref="http-requester-simple" path="api/odata.svc/orders(10248)?$format=atom">
</http:request>
</munit:execution>
<munit:validation>
<munit-tools:assert-that expression="#[output application/java --- attributes.headers.'Content-type']" is="#[MunitTools::equalTo('application/atom+xml')]"/>
<munit-tools:assert-that expression="#[output application/java --- payload.feed.id]" is="#[MunitTools::startsWith('http://ch-https-41-demo.au-s1.cloudhub.io')]"/>
<munit-tools:assert-that expression="#[output application/java --- payload.feed.entry.id]" is="#[MunitTools::startsWith('http://ch-https-41-demo.au-s1.cloudhub.io')]"/>
</munit:validation>
</munit:test>

<munit:test name="get-entity-by-id-with-atom-format-uri-without-full-domain" ignore="#['${fullDomain}' == 'ch-https-41-demo.au-s1.cloudhub.io']">
<munit:enable-flow-sources>
<munit:enable-flow-source value="success-server-main"/>
<munit:enable-flow-source value="get:\orders\(orderId):success-server-config"/>
</munit:enable-flow-sources>
<munit:execution>
<http:request method="GET" config-ref="http-requester-simple" path="api/odata.svc/orders(10248)?$format=atom">
</http:request>
</munit:execution>
<munit:validation>
<munit-tools:assert-that expression="#[output application/java --- attributes.headers.'Content-type']" is="#[MunitTools::equalTo('application/atom+xml')]"/>
<munit-tools:assert-that expression="#[output application/java --- payload.feed.id]" is="#[MunitTools::startsWith('http://localhost')]"/>
<munit-tools:assert-that expression="#[output application/java --- payload.feed.entry.id]" is="#[MunitTools::startsWith('http://localhost')]"/>
</munit:validation>
</munit:test>

</mule>

0 comments on commit 86e9e63

Please sign in to comment.