Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

org.mikeneck.httpspec:http-spec-runner

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

org.mikeneck.httpspec:http-spec-runner

Testing library for http API with DSL and jsonpath assertion.

  • v0.2.0
  • Source
  • Maven
  • Socket score

Version published
Maintainers
1
Source

http-spec

http-spec is a library for web services to describe REST API specification.

This library build on top of Java 11's http API.

Usage

(1) YAML base

spec:
  - name: 'getting path resource with paging parameters'
    request:
      get: 'http://localhost:8080/path'
      queries:
        q: test
        page: 4
        limit: 10
      headers:
        accept: application/json
        authorization: bearer 11aa22bb33cc
    response:
      status: 200
      headers:
        content-type: application/json
      body:
        - path: $.firstName
          expect:
            type: string
            value: John
class RestApiTest {
    @Test
    void runRequestFromYamlFile() {
        var yamlFile = new File("/path/to/spec.yaml");
        HttpSpecRunner.from(yamlFile).run(); // runs all requests and asserts responses.
    }
}

(2) Java base

class RestApiTest {
    @Test
    void runRequestByJavaCode() {
        HttpSpecRunner.Builder builder = HttpSpecRunner.builder();
        builder.addSpec(spec -> {
            spec.name("getting path resource with paging parameters");
            spec.request().get("http://localhost:8080/path", request -> {
                    request.query("q", "test");
                    request.query("page", 4);
                    request.query("limit", 10);
                    request.header("accept", "application/json");
                    request.header("authorization", "bearer 11aa22bb33cc");
            });
            spec.response(response -> {
                response.status(200);
                response.header("content-type", "application/json");
                response.jsonBody(jsonBody -> jsonBody.path("$.firstName").toBe("John"));
            });
        });
        HttpSpecRunner httpSpecRunner = builder.build();
        httpSpecRunner.run(); // runs all requests and asserts responses.
    }
}

junit-jupiter module

T.B.D.

With junit-jupiter module, you can convert HttpSpecRunner into Stream<DynamicTest>.

import java.util.stream.Stream;

class RestApiTest {
    // With junit-jupiter module, you can use httpSpecRunner as dynamic tests.
    @TestFactory
    Stream<DynamicTest> asDynamicTests() {
        HttpSpecRunner.Builder builder = HttpSpecRunner.builder();

        // same as the code in runRequestByJavaCode

        HttpSpecRunner httpSpecRunner = builder.build();
        // converts each assertion to dynamicTest
        //    - http status to be 200
        //    - content-type header to be application/json
        //    - body json has element $.firstName to be John 
        return DynamicTestAdapter.adapt(httpSpecRunner);
    }
}

FAQs

Package last updated on 22 May 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc