Some time ago I needed a webserver for functional testing of my java HTTP client library.
I looked for a web server which satisfy following conditions:
1. Embeddable - the main condition.
2. Easy to integrate with unit testing frameworks
3. Small amount of code and config to setup server
4. Quick startup and shutdown.
5. Small size of the library.
The first candidate which satisfy main condition is Jetty.
It is cool but it doesn't satisfy all other my conditions.
Next candidate is com.sun.net.httpserver.HttpServer which is embedded into JDK 1.6 and higher.
It is excellent candidate for my needs, so i decided to use it.
I've implemented some kind of abstraction layer over HttpServer to fully satisfify 2-nd and 3-d conditions and as a result i wrote very small library which i called anhttpserver (Another HTTP Server)
here is basic usage example of that library:
Now point your favorite browser to http://localhost:8000/index - you should see "Hello world".
Isn't it easy?
Let's write basic JUnit test which will show, how easy to write functional tests using this library:
More examples of using anhttpserver library you can see in source code of anhttpserver and anhttpclient projects which are open source and released under the MIT license.
Also there is one non usual example of usage of this library - jstreamserver (HTTP Live Streaming for IPad and IPhone).
Here i used anhttpclient not for functional testing but as an HTTP server.
For sure i do not recommend to use it in such way - i didn't perform any stability and performance testing, but this example is proof of the viability of anhttpserver.
You can download sources of anhttpserver here: https://github.com/sprilukin/anhttpserver
Thank you for attention,
Comments are welcome.
I looked for a web server which satisfy following conditions:
1. Embeddable - the main condition.
2. Easy to integrate with unit testing frameworks
3. Small amount of code and config to setup server
4. Quick startup and shutdown.
5. Small size of the library.
The first candidate which satisfy main condition is Jetty.
It is cool but it doesn't satisfy all other my conditions.
Next candidate is com.sun.net.httpserver.HttpServer which is embedded into JDK 1.6 and higher.
It is excellent candidate for my needs, so i decided to use it.
I've implemented some kind of abstraction layer over HttpServer to fully satisfify 2-nd and 3-d conditions and as a result i wrote very small library which i called anhttpserver (Another HTTP Server)
here is basic usage example of that library:
import anhttpserver.ByteArrayHandlerAdapter;
import anhttpserver.DefaultSimpleHttpServer;
import anhttpserver.HttpRequestContext;
import anhttpserver.SimpleHttpServer;
import java.io.IOException;
public class SimpleServer {
public static void main(String[] args) throws Exception {
SimpleHttpServer server = new DefaultSimpleHttpServer();
server.start();
server.addHandler("/index", new ByteArrayHandlerAdapter() {
public byte[] getResponseAsByteArray(HttpRequestContext httpRequestContext) throws IOException {
return "Hello world".getBytes();
}
});
}
}
import anhttpserver.DefaultSimpleHttpServer;
import anhttpserver.HttpRequestContext;
import anhttpserver.SimpleHttpServer;
import java.io.IOException;
public class SimpleServer {
public static void main(String[] args) throws Exception {
SimpleHttpServer server = new DefaultSimpleHttpServer();
server.start();
server.addHandler("/index", new ByteArrayHandlerAdapter() {
public byte[] getResponseAsByteArray(HttpRequestContext httpRequestContext) throws IOException {
return "Hello world".getBytes();
}
});
}
}
Now point your favorite browser to http://localhost:8000/index - you should see "Hello world".
Isn't it easy?
Let's write basic JUnit test which will show, how easy to write functional tests using this library:
import anhttpserver.ByteArrayHandlerAdapter;
import anhttpserver.DefaultSimpleHttpServer;
import anhttpserver.HttpRequestContext;
import anhttpserver.SimpleHttpServer;
import org.apache.commons.io.IOUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import static org.junit.Assert.assertEquals;
public class SimpleTest {
private SimpleHttpServer server;
@Before
public void init() {
server = new DefaultSimpleHttpServer();
server.start();
}
@After
public void finish() {
server.stop();
}
//In our example this is function we test,
//in your case most probablt it will be some other entity
private String getResult(String urlString) throws Exception {
URLConnection connection = (new URL(urlString)).openConnection();
InputStream is = connection.getInputStream();
String result = IOUtils.toString(is);
is.close();
return result;
}
@Test
public void basicServerTest() throws Exception {
server.addHandler("/", new ByteArrayHandlerAdapter() {
@Override
public byte[] getResponseAsByteArray(HttpRequestContext httpRequestContext) throws IOException {
String response = "Hello world!";
return response.getBytes();
}
});
assertEquals("Hello world!", getResult("http://localhost:8000"));
}
}
import anhttpserver.DefaultSimpleHttpServer;
import anhttpserver.HttpRequestContext;
import anhttpserver.SimpleHttpServer;
import org.apache.commons.io.IOUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import static org.junit.Assert.assertEquals;
public class SimpleTest {
private SimpleHttpServer server;
@Before
public void init() {
server = new DefaultSimpleHttpServer();
server.start();
}
@After
public void finish() {
server.stop();
}
//In our example this is function we test,
//in your case most probablt it will be some other entity
private String getResult(String urlString) throws Exception {
URLConnection connection = (new URL(urlString)).openConnection();
InputStream is = connection.getInputStream();
String result = IOUtils.toString(is);
is.close();
return result;
}
@Test
public void basicServerTest() throws Exception {
server.addHandler("/", new ByteArrayHandlerAdapter() {
@Override
public byte[] getResponseAsByteArray(HttpRequestContext httpRequestContext) throws IOException {
String response = "Hello world!";
return response.getBytes();
}
});
assertEquals("Hello world!", getResult("http://localhost:8000"));
}
}
More examples of using anhttpserver library you can see in source code of anhttpserver and anhttpclient projects which are open source and released under the MIT license.
Also there is one non usual example of usage of this library - jstreamserver (HTTP Live Streaming for IPad and IPhone).
Here i used anhttpclient not for functional testing but as an HTTP server.
For sure i do not recommend to use it in such way - i didn't perform any stability and performance testing, but this example is proof of the viability of anhttpserver.
You can download sources of anhttpserver here: https://github.com/sprilukin/anhttpserver
Thank you for attention,
Comments are welcome.
I was very interested in the article , it’s quite inspiring I should admit. I like visiting your site since I always come across interesting articles like this one. Keep sharing! Regards. Read more about
ОтветитьУдалитьVery valuable post...! This information shared is helpful to improve my knowledge skill. Thank you...!
Offshore software testing services
software testing services company
software testing services
Software Qa Services
quality assurance service providers
Performance testing services
Security testing services
software testing Companies
regression testing services
Absolutely fantastic posting! Lots of useful information and inspiration, both of which we all need!Relay appreciate your work.
ОтветитьУдалитьSoftware Testing Services
Functional Testing Services
Test Automation Services
QA Automation Testing Services
Regression Testing Services
API Testing Services
Compatibility Testing Services
Performance Testing Services
Security Testing Services
Vulnerability Testing Services
it helps more.
ОтветитьУдалитьSelenium Training in Chennai | Certification | Online Courses
selenium training in chennai
selenium training in chennai
selenium online training in chennai
selenium training in bangalore
selenium training in hyderabad
selenium training in coimbatore
selenium online training