Skip to content

Commit 33aa4dc

Browse files
http: header with map (#731)
1 parent 302eef7 commit 33aa4dc

2 files changed

Lines changed: 33 additions & 0 deletions

File tree

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,31 @@ public void add(String key, String value) {
9393
header.put(key, value);
9494
}
9595

96+
/**
97+
* Creates a new header from the current one with an additional key-value
98+
* @param key additional key
99+
* @param value additional value
100+
* @return new header
101+
*/
96102
public HttpHeader with(String key, CharSequence value) {
97103
Map<String, CharSequence> copy = new LinkedHashMap<>(this.header);
98104
copy.put(key, value);
99105

100106
return new HttpHeader(copy);
101107
}
102108

109+
/**
110+
* Creates a new header from the current one with an additional key values
111+
* @param additionalValues additional values
112+
* @return new header
113+
*/
114+
public HttpHeader with(Map<String, CharSequence> additionalValues) {
115+
Map<String, CharSequence> copy = new LinkedHashMap<>(this.header);
116+
copy.putAll(additionalValues);
117+
118+
return new HttpHeader(copy);
119+
}
120+
103121
public HttpHeader redactSecrets() {
104122
Map<String, CharSequence> redacted = new LinkedHashMap<>();
105123
for (Map.Entry<String, CharSequence> entry : header.entrySet()) {

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/*
2+
* Copyright 2020 webtau maintainers
23
* Copyright 2019 TWO SIGMA OPEN SOURCE, LLC
34
*
45
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -88,4 +89,18 @@ class HttpHeaderTest {
8889
def newHeader = header.with('foo', 'bar')
8990
newHeader.should == new HttpHeader(['foo': 'bar'])
9091
}
92+
93+
@Test
94+
void "build from existing header with a single new value"() {
95+
def header = new HttpHeader([k: 'v'])
96+
def newHeader = header.with('foo', 'bar')
97+
newHeader.should == new HttpHeader([k: 'v', 'foo': 'bar'])
98+
}
99+
100+
@Test
101+
void "build from existing header with a new map"() {
102+
def header = new HttpHeader([k: 'v'])
103+
def newHeader = header.with([k1: 'v1', k2: 'v2'])
104+
newHeader.should == new HttpHeader([k: 'v', k1: 'v1', k2: 'v2'])
105+
}
91106
}

0 commit comments

Comments
 (0)