From c6a6fa924d61d8e248a2b7edd5dfe4f1da24b424 Mon Sep 17 00:00:00 2001 From: Andrea Peruffo Date: Wed, 4 Oct 2023 11:14:21 +0100 Subject: [PATCH 1/2] Add a tryAdd method to RequestHeaders --- src/RequestHeaders.php | 17 +++++++++++++++++ tests/RequestHeadersTest.php | 11 +++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/RequestHeaders.php b/src/RequestHeaders.php index 64bae33..09b7f21 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): void + { + $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/tests/RequestHeadersTest.php b/tests/RequestHeadersTest.php index b60c9a1..b0bb7c4 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->add($key, "value2")); + $this->assertEquals(["value"], $headers->get($key)); + } + public function testCanPutAll(): void { $headers = new RequestHeaders(); From 85c56779a8d9a77cdfc7448418d6fbbb80f99071 Mon Sep 17 00:00:00 2001 From: Andrea Peruffo Date: Wed, 4 Oct 2023 11:30:13 +0100 Subject: [PATCH 2/2] Add a tryAdd method to RequestHeaders and use it in RequestInfo --- src/RequestHeaders.php | 2 +- src/RequestInformation.php | 10 +++++----- tests/RequestHeadersTest.php | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/RequestHeaders.php b/src/RequestHeaders.php index 09b7f21..52105f3 100644 --- a/src/RequestHeaders.php +++ b/src/RequestHeaders.php @@ -53,7 +53,7 @@ public function add(string $key, string $value): void * @param string $value * @return boolean if the value have been added */ - public function tryAdd(string $key, string $value): void + public function tryAdd(string $key, string $value): bool { $lowercaseKey = strtolower($key); if (array_key_exists($lowercaseKey, $this->headers)) { 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 b0bb7c4..9d2c86b 100644 --- a/tests/RequestHeadersTest.php +++ b/tests/RequestHeadersTest.php @@ -96,7 +96,7 @@ public function testCanTryAdd(): void $this->assertTrue($headers->tryAdd($key, "value")); $this->assertEquals(["value"], $headers->get($key)); - $this->assertFalse($headers->add($key, "value2")); + $this->assertFalse($headers->tryAdd($key, "value2")); $this->assertEquals(["value"], $headers->get($key)); }