Testing a HTTP GET from WAB

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Testing a HTTP GET from WAB

Massimo Bono
Hi,

I've generated a wab file containing a simple "hello world" index.html. Inside this wab there is the standard manifest with the WebContext-Path manifest entry.

When I manually start a karaf container, everything goes smoothly: I install the war feature (by using "feature:install war") and then I can successfully view the Webapp on a browser (http:/127.0.0.1:8181/hello-example).

However, things go south when I try to test automatically the webapp.
I've created a simple test (by extension KaratTestSupport) where I install the "war" feature using KarafDistributionOption.features and then start the webapp bundle (via CoreOptions.mavenBundle()). I'm using httpcomponents.httpclient (with a OSGi compliant manifest header) to request the HTTP GET connection; however, albeit http:list show the endpoint, I always obtain a "ConnectionRefused" (in the browser I cannot see anything as well).

Surely I'm doing something wrong. Is this the correct way to test a webapp?

Thanks for any kind reply!

--
Ing. Massimo Bono
Reply | Threaded
Open this post in threaded view
|

Re: Testing a HTTP GET from WAB

jbonofre
Hi

In the test, you can check with web:list and http:list if the status is deployed (and not deploying).
Maybe you test the connection before the endpoint is actually started.
You can see how we do it in the karaf examples itests.

Regards
JB

Le 16 nov. 2019 11:01, Massimo Bono <[hidden email]> a écrit :
Hi,

I've generated a wab file containing a simple "hello world" index.html. Inside this wab there is the standard manifest with the WebContext-Path manifest entry.

When I manually start a karaf container, everything goes smoothly: I install the war feature (by using "feature:install war") and then I can successfully view the Webapp on a browser (http:/127.0.0.1:8181/hello-example).

However, things go south when I try to test automatically the webapp.
I've created a simple test (by extension KaratTestSupport) where I install the "war" feature using KarafDistributionOption.features and then start the webapp bundle (via CoreOptions.mavenBundle()). I'm using httpcomponents.httpclient (with a OSGi compliant manifest header) to request the HTTP GET connection; however, albeit http:list show the endpoint, I always obtain a "ConnectionRefused" (in the browser I cannot see anything as well).

Surely I'm doing something wrong. Is this the correct way to test a webapp?

Thanks for any kind reply!

--
Ing. Massimo Bono

Reply | Threaded
Open this post in threaded view
|

Re: Testing a HTTP GET from WAB

Massimo Bono
just tried with web:list and http:list: the output seems fine but when i try to actually connect, I'm still experiencing Connection Refused.

http:list
ID  │ Servlet           │ Servlet-Name │ State       │ Alias           │ Url
────┼───────────────────┼──────────────┼─────────────┼─────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
140 │ ResourceServlet   │ default      │ Deployed    │ /hello-example/ │ [/hello-example/]
140 │ JspServletWrapper │ jsp          │ Deployed    │                 │ [/hello-example/*.jsp, /hello-example/*.jspx, /hello-example/*.jspf, /hello-example/*.xsp, /hello-example/*.JSP, /hello-example/*.JSPX, /hello-example/*.JSPF, /hello-example/*.XSP]

web:list
ID  │ State       │ Web-State   │ Level │ Web-ContextPath │ Name
────┼─────────────┼─────────────┼───────┼─────────────────┼────────────────
140 │ Active      │ Deployed    │ 81    │ /hello-example  │ task-ui (0.0.1)

I followed the example shown in https://github.com/apache/karaf/blob/master/itests/test/src/test/java/org/apache/karaf/itests/WebTest.java: to test, you call the command "feature:install war" and  "web:install" directly with executeCommand. Then I realized that the port open was not 8181 (as I was lead to believe from the WAR Deployer manual) but 9080 (Set from KarafTestSupport HTTP_PORT!). By using this.getHttpPort() method I was able to make it work!

Some code:

this.installAndAssertFeature("war");
stdout = this.executeCommand("web:install mvn:com.fibonacci.microsi/task-ui/0.0.1/jar hello-example");
System.out.println(stdout);

stdout = this.executeCommand("web:list", 5000L, false);
System.out.println(stdout);

this.installAndAssertFeature("war");
this.assertBundleInstalled("task-ui");
URL url = new URL("http://localhost:" + this.getHttpPort() + "/hello-example");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setRequestMethod("GET");
StringBuilder buffer = new StringBuilder();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
       String line = null;
       while ((line = reader.readLine()) != null) {
             buffer.append(line).append("\n");
       }
}
System.out.println(buffer.toString());

Thanks for pointing to the right direction!


Il giorno sab 16 nov 2019 alle ore 11:08 <[hidden email]> ha scritto:
Hi

In the test, you can check with web:list and http:list if the status is deployed (and not deploying).
Maybe you test the connection before the endpoint is actually started.
You can see how we do it in the karaf examples itests.

Regards
JB

Le 16 nov. 2019 11:01, Massimo Bono <[hidden email]> a écrit :
Hi,

I've generated a wab file containing a simple "hello world" index.html. Inside this wab there is the standard manifest with the WebContext-Path manifest entry.

When I manually start a karaf container, everything goes smoothly: I install the war feature (by using "feature:install war") and then I can successfully view the Webapp on a browser (http:/127.0.0.1:8181/hello-example).

However, things go south when I try to test automatically the webapp.
I've created a simple test (by extension KaratTestSupport) where I install the "war" feature using KarafDistributionOption.features and then start the webapp bundle (via CoreOptions.mavenBundle()). I'm using httpcomponents.httpclient (with a OSGi compliant manifest header) to request the HTTP GET connection; however, albeit http:list show the endpoint, I always obtain a "ConnectionRefused" (in the browser I cannot see anything as well).

Surely I'm doing something wrong. Is this the correct way to test a webapp?

Thanks for any kind reply!

--
Ing. Massimo Bono



--
Ing. Massimo Bono
Reply | Threaded
Open this post in threaded view
|

Re: Testing a HTTP GET from WAB

jbonofre
Hi

yes, if you extend KarafTestSupport, it automatically pick a free port.

So, you have to use getHttpPort() to get the actual port used.

Regards
JB

On 16/11/2019 12:01, Massimo Bono wrote:

> just tried with web:list and http:list: the output seems fine but when i
> try to actually connect, I'm still experiencing Connection Refused.
>
> http:list
>
>     ID  │ Servlet           │ Servlet-Name │ State       │ Alias        
>       │ Url
>     ────┼───────────────────┼──────────────┼─────────────┼─────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
>     140 │ ResourceServlet   │ default      │ Deployed    │
>     /hello-example/ │ [/hello-example/]
>     140 │ JspServletWrapper │ jsp          │ Deployed    │              
>       │ [/hello-example/*.jsp, /hello-example/*.jspx,
>     /hello-example/*.jspf, /hello-example/*.xsp, /hello-example/*.JSP,
>     /hello-example/*.JSPX, /hello-example/*.JSPF, /hello-example/*.XSP]
>
>
> web:list
>
>     ID  │ State       │ Web-State   │ Level │ Web-ContextPath │ Name
>     ────┼─────────────┼─────────────┼───────┼─────────────────┼────────────────
>     140 │ Active      │ Deployed    │ 81    │ /hello-example  │ task-ui
>     (0.0.1)
>
>
> I followed the example shown in
> https://github.com/apache/karaf/blob/master/itests/test/src/test/java/org/apache/karaf/itests/WebTest.java:
> to test, you call the command "feature:install war" and  "web:install"
> directly with executeCommand. Then I realized that the port open was not
> 8181 (as I was lead to believe from the WAR Deployer manual) but 9080
> (Set from KarafTestSupport HTTP_PORT!). By using this.getHttpPort()
> method I was able to make it work!
>
> Some code:
>
> this.installAndAssertFeature("war");
> stdout = this.executeCommand("web:install
> mvn:com.fibonacci.microsi/task-ui/0.0.1/jar hello-example");
> System.out.println(stdout);
>
> stdout = this.executeCommand("web:list", 5000L, false);
> System.out.println(stdout);
>
> this.installAndAssertFeature("war");
> this.assertBundleInstalled("task-ui");
> URL url = new URL("http://localhost:" + this.getHttpPort() +
> "/hello-example");
> HttpURLConnection connection = (HttpURLConnection) url.openConnection();
> connection.setDoInput(true);
> connection.setRequestMethod("GET");
> StringBuilder buffer = new StringBuilder();
> try (BufferedReader reader = new BufferedReader(new
> InputStreamReader(connection.getInputStream()))) {
>        String line = null;
>        while ((line = reader.readLine()) != null) {
>              buffer.append(line).append("\n");
>        }
> }
> System.out.println(buffer.toString());
>
> Thanks for pointing to the right direction!
>
>
> Il giorno sab 16 nov 2019 alle ore 11:08 <[hidden email]
> <mailto:[hidden email]>> ha scritto:
>
>     Hi
>
>     In the test, you can check with web:list and http:list if the status
>     is deployed (and not deploying).
>     Maybe you test the connection before the endpoint is actually started.
>     You can see how we do it in the karaf examples itests.
>
>     Regards
>     JB
>
>     Le 16 nov. 2019 11:01, Massimo Bono <[hidden email]
>     <mailto:[hidden email]>> a écrit :
>
>         Hi,
>
>         I've generated a wab file containing a simple "hello world"
>         index.html. Inside this wab there is the standard manifest with
>         the WebContext-Path manifest entry.
>
>         When I manually start a karaf container, everything goes
>         smoothly: I install the war feature (by using "feature:install
>         war") and then I can successfully view the Webapp on a browser
>         (http:/127.0.0.1:8181/hello-example
>         <http://127.0.0.1:8181/hello-example>).
>
>         However, things go south when I try to test automatically the
>         webapp.
>         I've created a simple test (by extension KaratTestSupport) where
>         I install the "war" feature using
>         KarafDistributionOption.features and then start the webapp
>         bundle (via CoreOptions.mavenBundle()). I'm using
>         httpcomponents.httpclient (with a OSGi compliant manifest
>         header) to request the HTTP GET connection; however, albeit
>         http:list show the endpoint, I always obtain a
>         "ConnectionRefused" (in the browser I cannot see anything as well).
>
>         Surely I'm doing something wrong. Is this the correct way to
>         test a webapp?
>
>         Thanks for any kind reply!
>
>         --
>         *Ing. Massimo Bono*
>
>
>
>
> --
> *Ing. Massimo Bono*

--
Jean-Baptiste Onofré
[hidden email]
http://blog.nanthrax.net
Talend - http://www.talend.com