vitalijr2 / ijhttp-tools

Maven plugin, Spring Boot Test autoconfiguration and jUnit Extension for IntelliJ HTTP Client
Apache License 2.0
3 stars 0 forks source link

Maven plugin, Spring Boot Test autoconfiguration and jUnit Extension for IntelliJ HTTP Client

I had started with a Maven plugin to run HTTP requests on the integration-test phase using the IntelliJ HTTP Client. Later I added a Spring Boot Test autoconfiguration, thanks @GoncaloPT for his idea. Next step was jUnit Extension.

Codacy Badge Codacy Coverage Java Version

Table of Contents

Created by gh-md-toc

Screenshot

Getting started

Originally the IntelliJ HTTP Client plugin allows to create, edit, and execute HTTP requests directly in the IntelliJ IDEA code editor. The IntelliJ HTTP Client is also available as a CLI tool.

The Maven plugin and jUnit Extension allow to run HTTP requests on the integration-test phase using the IntelliJ HTTP Client. The Spring Boot Test autoconfiguration allows to run them with Spring Boot Test, you don't need to package and run whole application.

The HTTP Request in Editor Specification describes format these files.

Example requests:

GET /api/get HTTP/1.1
Accept: application/json
Host: example.com

### Add an item
POST /api/add HTTP/1.1
Host: example.com
Content-Type: application/json

{
  "name": "entity",
  "value": "content"
}

Directories (extra feature)

IntelliJ HTTP Client needs HTTP files to work. With HTTP Client Command Line you can set directories that contain such files.

Usage

Important! plugin, extension and autoconfiguration do not contain the HTTP client: you need to install it by yourself then add to PATH. You can also set the full path to the ijhttp via the parameter executable. The HTTP Client Demo has some examples how to download the HTTP client.

Maven plugin

Maven Central Javadoc

There is one goal run. To use it add the plugin to your POM.

Example of full configuration:


<plugin>
  <groupId>io.gitlab.vitalijr2.ijhttp-tools</groupId>
  <artifactId>ijhttp-maven-plugin</artifactId>
  <version><!-- search on Maven Central --></version>
  <executions>
    <execution>
      <configuration>
        <!-- At least one file or directory is required. -->
        <directories>
          <directory>src/test/resources</directory>
        </directories>
        <environmentFile>public-env.json</environmentFile>
        <environmentName>dev</environmentName>
        <files>
          <file>sample-1-queries.http</file>
          <file>sample-2-queries.http</file>
        </files>
        <logLevel>HEADERS</logLevel>
        <report>true</report>
        <workingDirectory>target</workingDirectory>
      </configuration>
      <goals>
        <goal>run</goal>
      </goals>
      <id>simple-run-with-report</id>
    </execution>
  </executions>
</plugin>

To manage plugin's output use useMavenLogger, quietLogs and outputFile.

jUnit Extension

Maven Central Javadoc

Use annotations HttpClientExecutor and HttpClientCommandLineParameters to initialise and configure both executor and command line builder.

Example of full configuration:


@SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
@AutoConfigureHttpClientCommandLine(timeout = 7000)
class HttpClientCommandLineApplicationTests {

  @Autowired
  private Executor executor;

  @Autowired
  private HttpClientCommandLine httpClientCommandLine;

  @Test
  void httpClientCommandLine() throws IOException {
    // when
    var exitCode = executor.execute(httpClientCommandLine.getCommandLine());

    // then
    assertEquals(0, exitCode);
  }

}

Spring Boot Test autoconfiguration

Maven Central Javadoc

You can set configuration in application.yaml or manually, or combine both ways.

Example of autoconfiguration, full configuration:


ijhttp:
  parameters:
    connect-timeout: 9000
    directories:
      - src/test/resources/ijhttp
    # docker-mode: false default value
    environment-file: public-env.json
    environment-name: dev
    # executable: ijhttp default value
    files:
      - orders.http
      - products.http
      - checkout.http
    # insecure: false default value
    log-level: verbose
    private-environment-file: private-env.json
    # proxy: http://localhost:3128/
    report: true
    report-path: target/ijhttp
    socket-timeout: 9000
  # timeout: 7000

@SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
@AutoConfigureHttpClientCommandLine(timeout = 7000)
class HttpClientCommandLineApplicationTests {

  @Autowired
  private Executor executor;

  @Autowired
  private HttpClientCommandLine httpClientCommandLine;

  @Test
  void httpClientCommandLine() throws IOException {
    // when
    var exitCode = executor.execute(httpClientCommandLine.getCommandLine());

    // then
    assertEquals(0, exitCode);
  }

}

Contributing

Please read Contributing.

History

See Changelog

License

Copyright 2023-2024 Vitalij Berdinskih

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Apache License v2.0 License