Giter Club home page Giter Club logo

http-server's Introduction

HTTP Server

Build Status Coverage Status Maven Central

HTTP Server is a very simple embeddable HTTP server written in Java with very few dependencies. Its original intent is to be used in JUnit tests so you can verify your code properly made HTTP requests and that the request was formatted properly.

Background

I wrote a simple SlackNotification class for work, but I had no easy way to test that it was working properly. At first, I sent a message on Slack telling everyone I was testing and to ignore the next 50 messages or so. That works, but a better test wouldn't bother all of my coworkers and could be run as many times as I wanted, preferably when the rest of our stuff is built. Besides, I wanted to make sure my code worked as a unit test. Actually hitting Slack during testing is more of an integration level test.

Typical Use

HTTP Server implements Closeable. Use a try-with-resources statement to keeps things simple and tidy. After start() is called, HTTP server binds to an open port by default. Then call your code to be tested passing in "localhost" and the port that HTTP Server bound to. After your code under test is finished, you can inspect the requests and responses to make sure things were put together correctly.

@Test
void testSlackNotification() throws SlackException {
	try ( HTTPServer server = HTTPServer.always200OK() ) {

		// call some code you wrote that makes an HTTP call
		SlackNotification slack = new SlackNotification();
		slack.setUrl( "http://localhost:" + server.getPort() );
		slack.setMessage( "Hello world" );
		slack.setEmoji( ":tada:" );
		
		// more setup
		
		slack.send();
		
		// now assert everything looks like it should
		List<HTTPRequest> requests = server.getRequests();
		Assert.assertFalse( requests.isEmpty() );
		Assert.assertTrue( requests.get(0).getBodyAsString().contains( "Hello world" ) );
		
		// more asserts
	}

}

More Advanced Use

Concurrency

Using TestNG so you can run concurrent tests? Run as many of these servers as you like simultaneously as long as you have enough memory and ports open (there are 65,535 of them).

Repeatability

Don't want to disturb your coworkers or have to clean up a website after your unit tests run? Test against this server.

Custom Request Handling

Life isn't always 200 OK. Eventually something is going to blow up. Why not test for it? Implement the HTTPRequestHandler interface so your test server responds exactly like you want.

For example, what if you want to test how your code handles errors from the server?

@Test
void testInvalidSlackToken() {
	try ( HTTPServer server = new HTTPServer() ) {
	
		// make the server tell us that we sent bad data
		server.setHTTPRequestHandler( (request, response) -> response.setStatus( 400 ) );
		server.start();
	
		// call some code you wrote that makes an HTTP call
		SlackNotification slack = new SlackNotification();
		slack.setUrl( "http://localhost:" + server.getPort() );
		slack.setToken( "obviously not a real slack token" );
		slack.setMessage( "Hello world" );
		slack.setEmoji( ":tada:" );

		assertThrows( SlackException.class, () -> slack.send() );
	}

}

http-server's People

Contributors

bmauter avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.