Giter VIP home page Giter VIP logo

Comments (4)

psh avatar psh commented on July 29, 2024

You have a couple of options:

  • src/test/resources is mapped into the classpath by Maven when you run tests. You can get an input stream by getting a reference to your nanohttpd server, and calling getResourceAsStream()
    @Override public Response serve(HTTPSession session) {
        String uri = session.getUri();
        InputStream stream = getClass().getResourceAsStream(uri);
        if (stream == null) {
            return new Response(Response.Status.NOT_FOUND, MIME_PLAINTEXT, "Not Found: "+uri);
        }
        return new Response(Response.Status.OK, MIME_HTML, stream);
    }
  • Alternatively the starting base directory when running tests is usually the project itself. That means you could prepend "src/test/resources" onto the URI and go looking for a file at that location.

from nanohttpd.

apennebaker avatar apennebaker commented on July 29, 2024

@psh Thanks!

One more thing, not all the files will be HTML. Can this be generalized to arbitrary mimetypes?

from nanohttpd.

psh avatar psh commented on July 29, 2024

The constant being passed for the mime type is just a String, so feel free to use whatever suits the file you have. Check out the "webserver" sub-project for code that associates file extensions to their appropriate mime type if you need an idea of what to do.

from nanohttpd.

apennebaker avatar apennebaker commented on July 29, 2024

@psh Thanks again!

Could you tweak the example to make use of webserver associations?

from nanohttpd.

Related Issues (20)

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.