diff --git a/src/RequestHeaders.php b/src/RequestHeaders.php index 64bae33..52105f3 100644 --- a/src/RequestHeaders.php +++ b/src/RequestHeaders.php @@ -47,6 +47,23 @@ public function add(string $key, string $value): void } } + /** + * Try to add a value to the existing values for that specific header if it's not already set. + * @param string $key + * @param string $value + * @return boolean if the value have been added + */ + public function tryAdd(string $key, string $value): bool + { + $lowercaseKey = strtolower($key); + if (array_key_exists($lowercaseKey, $this->headers)) { + return false; + } else { + $this->headers[$lowercaseKey] = [$value => true]; + return true; + } + } + /** * Returns the lowercase version of a string. * @param string $key diff --git a/src/RequestInformation.php b/src/RequestInformation.php index bab234d..baae631 100644 --- a/src/RequestInformation.php +++ b/src/RequestInformation.php @@ -153,7 +153,7 @@ public function removeRequestOptions(RequestOption ...$options): void { */ public function setStreamContent(StreamInterface $value): void { $this->content = $value; - $this->headers->add(self::$contentTypeHeader, self::$binaryContentType); + $this->headers->tryAdd(self::$contentTypeHeader, self::$binaryContentType); } /** @@ -171,7 +171,7 @@ public function setContentFromParsable(RequestAdapter $requestAdapter, string $c $writer = $requestAdapter->getSerializationWriterFactory()->getSerializationWriter($contentType); $writer->writeObjectValue(null, $value); $span->setAttribute(ObservabilityOptions::REQUEST_TYPE_KEY, get_class($value)); - $this->headers->add(self::$contentTypeHeader, $contentType); + $this->headers->tryAdd(self::$contentTypeHeader, $contentType); $this->content = $writer->getSerializedContent(); $span->setStatus(StatusCode::STATUS_OK); } catch (Exception $exception) { @@ -205,7 +205,7 @@ public function setContentFromParsableCollection(RequestAdapter $requestAdapter, if (!empty($values)) { $span->setAttribute(ObservabilityOptions::REQUEST_TYPE_KEY, get_class($values[0])); } - $this->headers->add(self::$contentTypeHeader, $contentType); + $this->headers->tryAdd(self::$contentTypeHeader, $contentType); $this->content = $writer->getSerializedContent(); } catch (Exception $exception) { throw new RuntimeException('could not serialize payload.', 1, $exception); @@ -232,7 +232,7 @@ public function setContentFromScalar(RequestAdapter $requestAdapter, string $con $writer->writeAnyValue(null, $value); $span->setAttribute(self::$contentTypeHeader, $contentType); $span->setAttribute(ObservabilityOptions::REQUEST_TYPE_KEY, gettype($value)); - $this->headers->add(self::$contentTypeHeader, $contentType); + $this->headers->tryAdd(self::$contentTypeHeader, $contentType); $this->content = $writer->getSerializedContent(); $span->setStatus(StatusCode::STATUS_OK); } catch (Exception $exception) { @@ -265,7 +265,7 @@ public function setContentFromScalarCollection(RequestAdapter $requestAdapter, s if (!empty($values)) { $span->setAttribute(ObservabilityOptions::REQUEST_TYPE_KEY, gettype($values[0])); } - $this->headers->add(self::$contentTypeHeader, $contentType); + $this->headers->tryAdd(self::$contentTypeHeader, $contentType); $this->content = $writer->getSerializedContent(); $span->setStatus(StatusCode::STATUS_OK); } catch (Exception $exception) { diff --git a/tests/RequestHeadersTest.php b/tests/RequestHeadersTest.php index b60c9a1..9d2c86b 100644 --- a/tests/RequestHeadersTest.php +++ b/tests/RequestHeadersTest.php @@ -89,6 +89,17 @@ public function testCanAdd(): void $this->assertEquals(["value", "value2", "VALUE2"], $headers->get($key)); } + public function testCanTryAdd(): void + { + $headers = new RequestHeaders(); + $key = "key"; + $this->assertTrue($headers->tryAdd($key, "value")); + $this->assertEquals(["value"], $headers->get($key)); + + $this->assertFalse($headers->tryAdd($key, "value2")); + $this->assertEquals(["value"], $headers->get($key)); + } + public function testCanPutAll(): void { $headers = new RequestHeaders();