Skip to content

Commit c0530bc

Browse files
http: resource header support (#1444)
1 parent c2ebf90 commit c0530bc

File tree

6 files changed

+66
-11
lines changed

6 files changed

+66
-11
lines changed

webtau-docs/znai/HTTP/HTTP-resource.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,23 @@ Java:
113113
surroundedBy: "full-text-response"
114114
}
115115
```
116+
117+
# HTTP Header
118+
119+
Use `http.resource` optional second parameter to pass HTTP header:
120+
121+
```tabs
122+
Groovy:
123+
:include-file: org/testingisdocumenting/webtau/http/HttpResourceGroovyTest.groovy {
124+
title: "resource header",
125+
surroundedBy: "pass-header",
126+
replace: [["full-echo", "end-point"], ['get\\("x-prop"\\)', 'path']]
127+
}
128+
129+
Java:
130+
:include-file: org/testingisdocumenting/webtau/http/HttpResourceJavaTest.java {
131+
title: "resource header",
132+
surroundedBy: "pass-header",
133+
replace: [["full-echo", "end-point"], ['"x-prop"', '"path"']]
134+
}
135+
```

webtau-http/src/main/java/org/testingisdocumenting/webtau/http/Http.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,23 @@ public class Http {
8989
* WebTau has a way to define a lazy value associated with <code>HTTP GET</code> response. After that it can be used in multiple tests, <code>should</code> and <code>waitTo</code> on it.
9090
* <p>
9191
* Value can be associated with static urls like <code>/info</code> or dynamic urls like <code>/price/:ticker</code>
92-
* @param definitionGet resource defintion with optional placeholders
92+
* @param definitionGet resource definition with optional placeholders
9393
* @return resource definition
9494
*/
9595
public HttpResource resource(String definitionGet) {
96-
return new HttpResource(definitionGet);
96+
return new HttpResource(definitionGet, HttpHeader.EMPTY);
97+
}
98+
99+
/**
100+
* WebTau has a way to define a lazy value associated with <code>HTTP GET</code> response. After that it can be used in multiple tests, <code>should</code> and <code>waitTo</code> on it.
101+
* <p>
102+
* Value can be associated with static urls like <code>/info</code> or dynamic urls like <code>/price/:ticker</code>
103+
* @param definitionGet resource definition with optional placeholders
104+
* @param header header to use for the calls
105+
* @return resource definition
106+
*/
107+
public HttpResource resource(String definitionGet, HttpHeader header) {
108+
return new HttpResource(definitionGet, header);
97109
}
98110

99111
public boolean ping(String url) {

webtau-http/src/main/java/org/testingisdocumenting/webtau/http/resource/HttpLazyResponseValue.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.testingisdocumenting.webtau.http.Http;
2424
import org.testingisdocumenting.webtau.data.datanode.DataNodeId;
2525
import org.testingisdocumenting.webtau.data.datanode.DataNodeReturnNoConversionWrapper;
26+
import org.testingisdocumenting.webtau.http.HttpHeader;
2627
import org.testingisdocumenting.webtau.reporter.StepReportOptions;
2728
import org.testingisdocumenting.webtau.reporter.TokenizedMessage;
2829
import org.testingisdocumenting.webtau.utils.CollectionUtils;
@@ -34,12 +35,14 @@
3435
import static org.testingisdocumenting.webtau.WebTauCore.*;
3536

3637
public class HttpLazyResponseValue implements ActualValueExpectations, ActualValueAware, ActualPathAndDescriptionAware {
38+
private final HttpHeader _webtauInternalHeader;
3739
// we use _webtauInternal to avoid automatic groovy properties resolution and clash when using shortcut like resource.list[0].id
3840
private final DataNodeId _webtauInternalId;
3941
private final HttpResourceDefinition _webtauInternalResourceDefinition;
4042

41-
HttpLazyResponseValue(HttpResourceDefinition resourceDefinition, DataNodeId id) {
43+
HttpLazyResponseValue(HttpResourceDefinition resourceDefinition, HttpHeader header, DataNodeId id) {
4244
this._webtauInternalResourceDefinition = resourceDefinition;
45+
this._webtauInternalHeader = header;
4346
this._webtauInternalId = id;
4447
}
4548

@@ -49,7 +52,7 @@ public class HttpLazyResponseValue implements ActualValueExpectations, ActualVal
4952
* @return lazy response value
5053
*/
5154
public HttpLazyResponseValue get(String path) {
52-
return new HttpLazyResponseValue(_webtauInternalResourceDefinition, _webtauInternalId.concat(path));
55+
return new HttpLazyResponseValue(_webtauInternalResourceDefinition, _webtauInternalHeader, _webtauInternalId.concat(path));
5356
}
5457

5558
/**
@@ -58,7 +61,7 @@ public HttpLazyResponseValue get(String path) {
5861
* @return lazy response value
5962
*/
6063
public HttpLazyResponseValue get(int idx) {
61-
return new HttpLazyResponseValue(_webtauInternalResourceDefinition, _webtauInternalId.peer(idx));
64+
return new HttpLazyResponseValue(_webtauInternalResourceDefinition, _webtauInternalHeader, _webtauInternalId.peer(idx));
6265
}
6366

6467
/**
@@ -82,9 +85,11 @@ public boolean isDisabledMatcherStepOutput() {
8285

8386
@Override
8487
public Object actualValue() {
85-
DataNodeReturnNoConversionWrapper returnWrapper = Http.http.get(_webtauInternalResourceDefinition.buildUrl(), (header, body) -> _webtauInternalId.getPath().isEmpty() ?
86-
new DataNodeReturnNoConversionWrapper(body):
87-
new DataNodeReturnNoConversionWrapper(body.get(_webtauInternalId.getPath())));
88+
DataNodeReturnNoConversionWrapper returnWrapper = Http.http.get(_webtauInternalResourceDefinition.buildUrl(),
89+
_webtauInternalHeader,
90+
(header, body) -> _webtauInternalId.getPath().isEmpty() ?
91+
new DataNodeReturnNoConversionWrapper(body):
92+
new DataNodeReturnNoConversionWrapper(body.get(_webtauInternalId.getPath())));
8893

8994
return returnWrapper.getDataNode();
9095
}
@@ -129,11 +134,11 @@ public HttpLazyResponseValue of(Map<String, Object> routeParams) {
129134
paramNames + ", given: " + routeParams.keySet());
130135
}
131136

132-
return new HttpLazyResponseValue(_webtauInternalResourceDefinition.withRouteParams(routeParams), _webtauInternalId);
137+
return new HttpLazyResponseValue(_webtauInternalResourceDefinition.withRouteParams(routeParams), _webtauInternalHeader, _webtauInternalId);
133138
}
134139

135140
public String fullTextResponse() {
136-
return Http.http.get(_webtauInternalResourceDefinition.buildUrl(), (header, body) -> {
141+
return Http.http.get(_webtauInternalResourceDefinition.buildUrl(), _webtauInternalHeader, (header, body) -> {
137142
return body.getTextContent();
138143
});
139144
}

webtau-http/src/main/java/org/testingisdocumenting/webtau/http/resource/HttpResource.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,17 @@
1717
package org.testingisdocumenting.webtau.http.resource;
1818

1919
import org.testingisdocumenting.webtau.data.datanode.DataNodeId;
20+
import org.testingisdocumenting.webtau.http.HttpHeader;
2021

2122
import java.util.Collections;
2223

2324
public class HttpResource {
2425
public final HttpLazyResponseValue body;
2526

26-
public HttpResource(String definitionGet) {
27+
public HttpResource(String definitionGet, HttpHeader header) {
2728
this.body = new HttpLazyResponseValue(
2829
new HttpResourceDefinition(definitionGet, Collections.emptyMap()),
30+
header,
2931
new DataNodeId());
3032
}
3133

webtau-http/src/test/groovy/org/testingisdocumenting/webtau/http/HttpResourceGroovyTest.groovy

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,12 @@ class HttpResourceGroovyTest extends HttpTestBase {
9191
responseAsText.should == "{\"price\": 100}"
9292
// full-text-response
9393
}
94+
95+
@Test
96+
void "header"() {
97+
// pass-header
98+
def responseValue = http.resource("/full-echo", http.header("x-prop", "x-value")).get("x-prop")
99+
// pass-header
100+
responseValue.should(equal("x-value"))
101+
}
94102
}

webtau-http/src/test/java/org/testingisdocumenting/webtau/http/HttpResourceJavaTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,4 +232,12 @@ public void extractTextResponse() {
232232
actual(responseAsText, "response").should(equal("{\"price\": 100}"));
233233
// full-text-response
234234
}
235+
236+
@Test
237+
public void header() {
238+
// pass-header
239+
HttpLazyResponseValue responseValue = http.resource("/full-echo", http.header("x-prop", "x-value")).get("x-prop");
240+
// pass-header
241+
responseValue.should(equal("x-value"));
242+
}
235243
}

0 commit comments

Comments
 (0)