or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

http-testing.mdindex.mdlaunchers.mdtest-annotations.mdtest-context.mdtest-resources.mdutilities.md

http-testing.mddocs/

0

# HTTP Testing Support

1

2

Specialized support for HTTP endpoint testing with automatic URL injection and endpoint configuration. Integrates seamlessly with REST Assured and other HTTP testing libraries, providing convenient access to test server URLs and endpoints.

3

4

## Capabilities

5

6

### TestHTTPResource Annotation

7

8

Injects HTTP resources configured for test URLs, supporting various protocols and management interfaces.

9

10

```java { .api }

11

@Target({ElementType.FIELD})

12

@Retention(RetentionPolicy.RUNTIME)

13

public @interface TestHTTPResource {

14

String value() default "";

15

@Deprecated(since = "3.10", forRemoval = true)

16

boolean ssl() default false;

17

boolean tls() default false;

18

boolean management() default false;

19

}

20

```

21

22

**Basic Usage:**

23

```java

24

public class UserEndpointTest {

25

@TestHTTPResource

26

URL baseUrl;

27

28

@TestHTTPResource("/api/users")

29

URL usersEndpoint;

30

31

@TestHTTPResource(value = "/admin", management = true)

32

URL adminEndpoint;

33

34

@TestHTTPResource(value = "/secure", tls = true)

35

URL secureEndpoint;

36

37

@Test

38

public void testUserCreation() {

39

given()

40

.baseUri(usersEndpoint.toString())

41

.contentType(ContentType.JSON)

42

.body(new User("John", "john@example.com"))

43

.when()

44

.post()

45

.then()

46

.statusCode(201);

47

}

48

}

49

```

50

51

### TestHTTPEndpoint Annotation

52

53

Specifies the endpoint being tested for automatic URL configuration and path resolution.

54

55

```java { .api }

56

@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD})

57

@Retention(RetentionPolicy.RUNTIME)

58

public @interface TestHTTPEndpoint {

59

Class<?> value();

60

}

61

```

62

63

**Usage with JAX-RS Resources:**

64

```java

65

@Path("/api/users")

66

public class UserResource {

67

@GET

68

public List<User> getUsers() { /* implementation */ }

69

70

@POST

71

public Response createUser(User user) { /* implementation */ }

72

}

73

74

@TestHTTPEndpoint(UserResource.class)

75

public class UserResourceTest {

76

@TestHTTPResource

77

URL userResourceUrl; // Automatically configured to /api/users

78

79

@TestHTTPResource("/search")

80

URL searchUrl; // Relative to UserResource path: /api/users/search

81

82

@Test

83

public void testGetUsers() {

84

given()

85

.baseUri(userResourceUrl.toString())

86

.when()

87

.get()

88

.then()

89

.statusCode(200)

90

.contentType(ContentType.JSON);

91

}

92

}

93

```

94

95

### TestHTTPResourceProvider Interface

96

97

SPI for providing custom HTTP resource types beyond the built-in URL, URI, and String providers.

98

99

```java { .api }

100

public interface TestHTTPResourceProvider<T> {

101

Class<T> getProvidedType();

102

T provide(String testUri, Field field);

103

}

104

```

105

106

**Custom Provider Implementation:**

107

```java

108

public class RestAssuredRequestSpecProvider implements TestHTTPResourceProvider<RequestSpecification> {

109

@Override

110

public Class<RequestSpecification> getProvidedType() {

111

return RequestSpecification.class;

112

}

113

114

@Override

115

public RequestSpecification provide(String testUri, Field field) {

116

return new RequestSpecBuilder()

117

.setBaseUri(testUri)

118

.setContentType(ContentType.JSON)

119

.build();

120

}

121

}

122

123

// Usage in test:

124

public class APITest {

125

@TestHTTPResource

126

RequestSpecification requestSpec; // Automatically configured

127

128

@Test

129

public void testWithCustomSpec() {

130

requestSpec

131

.body(testData)

132

.when()

133

.post("/api/endpoint")

134

.then()

135

.statusCode(200);

136

}

137

}

138

```

139

140

## Built-in HTTP Resource Providers

141

142

The framework includes several built-in providers for common HTTP resource types:

143

144

### String Provider

145

```java

146

public class StringTestHTTPResourceProvider implements TestHTTPResourceProvider<String> {

147

@Override

148

public Class<String> getProvidedType() { return String.class; }

149

150

@Override

151

public String provide(String testUri, Field field) { return testUri; }

152

}

153

```

154

155

### URL Provider

156

```java

157

public class URLTestHTTPResourceProvider implements TestHTTPResourceProvider<URL> {

158

@Override

159

public Class<URL> getProvidedType() { return URL.class; }

160

161

@Override

162

public URL provide(String testUri, Field field) {

163

try {

164

return new URL(testUri);

165

} catch (MalformedURLException e) {

166

throw new RuntimeException(e);

167

}

168

}

169

}

170

```

171

172

### URI Provider

173

```java

174

public class URITestHTTPResourceProvider implements TestHTTPResourceProvider<URI> {

175

@Override

176

public Class<URI> getProvidedType() { return URI.class; }

177

178

@Override

179

public URI provide(String testUri, Field field) {

180

return URI.create(testUri);

181

}

182

}

183

```

184

185

## Advanced Usage Patterns

186

187

### Testing Different Protocols

188

189

Configure resources for different protocols and security settings:

190

191

```java

192

public class SecurityTest {

193

@TestHTTPResource(value = "/public", tls = false)

194

URL publicEndpoint;

195

196

@TestHTTPResource(value = "/secure", tls = true)

197

URL secureEndpoint;

198

199

@TestHTTPResource(value = "/health", management = true)

200

URL managementEndpoint;

201

202

@Test

203

public void testSecureAccess() {

204

// Test HTTPS endpoint

205

given()

206

.baseUri(secureEndpoint.toString())

207

.relaxedHTTPSValidation()

208

.when()

209

.get()

210

.then()

211

.statusCode(200);

212

}

213

}

214

```

215

216

### RestAssuredURLManager Class

217

218

Utility class for managing RestAssured configuration in Quarkus tests, providing automatic URL setup, protocol management, and timeout configuration.

219

220

```java { .api }

221

public class RestAssuredURLManager {

222

public static void setURL(boolean useSecureConnection) {}

223

public static void setURL(boolean useSecureConnection, String additionalPath) {}

224

public static void setURL(boolean useSecureConnection, Integer port) {}

225

public static void setURL(boolean useSecureConnection, Integer port, String additionalPath) {}

226

public static void clearURL() {}

227

}

228

```

229

230

**Usage Examples:**

231

232

```java

233

public class RestAssuredConfigTest {

234

@BeforeEach

235

public void setup() {

236

// Configure RestAssured for HTTP testing

237

RestAssuredURLManager.setURL(false);

238

}

239

240

@AfterEach

241

public void cleanup() {

242

// Clear RestAssured configuration

243

RestAssuredURLManager.clearURL();

244

}

245

246

@Test

247

public void testWithCustomPath() {

248

// Configure with additional path

249

RestAssuredURLManager.setURL(false, "/api/v1");

250

251

given()

252

.when()

253

.get("/users")

254

.then()

255

.statusCode(200);

256

}

257

258

@Test

259

public void testWithSSL() {

260

// Configure for HTTPS testing

261

RestAssuredURLManager.setURL(true);

262

263

given()

264

.relaxedHTTPSValidation()

265

.when()

266

.get("/secure-endpoint")

267

.then()

268

.statusCode(200);

269

}

270

271

@Test

272

public void testWithCustomPort() {

273

// Configure with specific port and path

274

RestAssuredURLManager.setURL(false, 9090, "/admin");

275

276

given()

277

.when()

278

.get("/status")

279

.then()

280

.statusCode(200);

281

}

282

}

283

```

284

285

### Configuration Providers

286

287

Test HTTP configuration is provided through specialized configuration classes that automatically setup test URLs and properties.

288

289

```java { .api }

290

public class TestHTTPConfigSourceProvider implements ConfigSourceProvider {

291

public static final String TEST_URL_KEY = "test.url";

292

public static final String TEST_URL_SSL_KEY = "test.url.ssl";

293

public static final String TEST_MANAGEMENT_URL_KEY = "test.management.url";

294

public static final String TEST_MANAGEMENT_URL_SSL_KEY = "test.management.url.ssl";

295

public static final String HTTP_ROOT_PATH_KEY = "quarkus.http.root-path";

296

public static final String MANAGEMENT_ROOT_PATH_KEY = "quarkus.http.management-path";

297

298

public Iterable<ConfigSource> getConfigSources(ClassLoader forClassLoader) {}

299

}

300

301

public class TestHTTPConfigSourceInterceptor extends ExpressionConfigSourceInterceptor {

302

public ConfigValue getValue(ConfigSourceInterceptorContext context, String name) {}

303

}

304

```

305

306

**Usage with Configuration:**

307

308

```java

309

public class ConfigAwareTest {

310

@ConfigProperty(name = "test.url")

311

String testUrl;

312

313

@ConfigProperty(name = "test.url.ssl")

314

Optional<String> testSslUrl;

315

316

@ConfigProperty(name = "test.management.url")

317

Optional<String> managementUrl;

318

319

@Test

320

public void testConfigProperties() {

321

// The test URLs are automatically configured

322

assertNotNull(testUrl);

323

assertTrue(testUrl.startsWith("http://"));

324

325

// Management URL is available if management interface is enabled

326

managementUrl.ifPresent(url -> {

327

assertTrue(url.contains("management"));

328

});

329

}

330

331

@Test

332

public void testSSLConfiguration() {

333

// SSL URLs are provided when TLS is enabled

334

testSslUrl.ifPresent(url -> {

335

assertTrue(url.startsWith("https://"));

336

});

337

}

338

}

339

```

340

341

### Resource Injection with REST Assured

342

343

Common patterns for REST Assured integration:

344

345

```java

346

public class RestAssuredTest {

347

@TestHTTPResource

348

URL baseUrl;

349

350

@BeforeEach

351

public void setup() {

352

RestAssured.baseURI = baseUrl.toString();

353

RestAssured.defaultParser = Parser.JSON;

354

}

355

356

@Test

357

public void testCRUDOperations() {

358

// Create

359

String userId = given()

360

.contentType(ContentType.JSON)

361

.body(new User("Alice", "alice@example.com"))

362

.when()

363

.post("/api/users")

364

.then()

365

.statusCode(201)

366

.extract()

367

.path("id");

368

369

// Read

370

given()

371

.when()

372

.get("/api/users/{id}", userId)

373

.then()

374

.statusCode(200)

375

.body("name", equalTo("Alice"));

376

377

// Update

378

given()

379

.contentType(ContentType.JSON)

380

.body(Map.of("name", "Alice Updated"))

381

.when()

382

.put("/api/users/{id}", userId)

383

.then()

384

.statusCode(200);

385

386

// Delete

387

given()

388

.when()

389

.delete("/api/users/{id}", userId)

390

.then()

391

.statusCode(204);

392

}

393

}

394

```

395

396

### WebSocket and SSE Testing

397

398

Testing WebSocket and Server-Sent Events endpoints:

399

400

```java

401

public class WebSocketTest {

402

@TestHTTPResource("/websocket")

403

URI websocketUri;

404

405

@TestHTTPResource("/events")

406

URL eventsUrl;

407

408

@Test

409

public void testWebSocketConnection() {

410

WebSocketContainer container = ContainerProvider.getWebSocketContainer();

411

Session session = container.connectToServer(

412

new MyWebSocketClient(),

413

websocketUri

414

);

415

416

// Test WebSocket communication

417

session.getBasicRemote().sendText("Hello");

418

// Verify response...

419

}

420

421

@Test

422

public void testServerSentEvents() {

423

given()

424

.accept("text/event-stream")

425

.when()

426

.get(eventsUrl.toString())

427

.then()

428

.statusCode(200)

429

.contentType("text/event-stream");

430

}

431

}

432

```

433

434

### Testing with TestContainers

435

436

Combining HTTP testing with external services:

437

438

```java

439

@QuarkusTestResource(DatabaseTestResource.class)

440

public class IntegrationTest {

441

@TestHTTPResource("/api/users")

442

URL usersApi;

443

444

@Test

445

public void testUserPersistence() {

446

// Create user via HTTP API

447

String userId = given()

448

.baseUri(usersApi.toString())

449

.contentType(ContentType.JSON)

450

.body(new User("Bob", "bob@example.com"))

451

.when()

452

.post()

453

.then()

454

.statusCode(201)

455

.extract()

456

.path("id");

457

458

// Verify user exists via API

459

given()

460

.baseUri(usersApi.toString())

461

.when()

462

.get("/{id}", userId)

463

.then()

464

.statusCode(200)

465

.body("email", equalTo("bob@example.com"));

466

}

467

}

468

```