mdedetrich commented on code in PR #1246: URL: https://github.com/apache/pekko-connectors/pull/1246#discussion_r2522028113
########## s3/src/test/scala/org/apache/pekko/stream/connectors/s3/S3HeadersSpec.scala: ########## @@ -0,0 +1,102 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.pekko.stream.connectors.s3 + +import org.scalatest.flatspec.AnyFlatSpecLike +import org.scalatest.matchers.should.Matchers +import org.apache.pekko.stream.connectors.s3.impl.S3Request +import com.typesafe.config.ConfigFactory +import scala.reflect.runtime.universe._ + +class S3HeadersSpec extends AnyFlatSpecLike with Matchers { + it should "filter headers based on what's allowed" in { + val testOverrideConfig = ConfigFactory.parseString(""" + | pekko.connectors.s3.additional-allowed-headers { + | GetObject = [allowedExtra] + | HeadObject = [allowedExtra] + | PutObject = [allowedExtra] + | InitiateMultipartUpload = [allowedExtra] + | UploadPart = [allowedExtra] + | CopyPart = [allowedExtra] + | DeleteObject = [allowedExtra] + | ListBucket = [allowedExtra] + | MakeBucket = [allowedExtra] + | DeleteBucket = [allowedExtra] + | CheckBucket = [allowedExtra] + | PutBucketVersioning = [allowedExtra] + | GetBucketVersioning = [allowedExtra] + | } + |""".stripMargin) + + val defaultConfig = ConfigFactory.load() + val finalConfig = testOverrideConfig.withFallback(defaultConfig) + + S3Request.allRequests.foreach { + requestType => + val allowedHeaders = requestType.allowedHeaders.zipWithIndex.toMap.view.mapValues(_.toString()).toMap + val extraHeaders = Map("allowedExtra" -> "allGood", "notAllowed" -> "shouldBeGone") + val header = S3Headers().withCustomHeaders(allowedHeaders ++ extraHeaders) + val s3Config = finalConfig.getConfig("pekko.connectors.s3") + val headerFilter = header.headersFor(requestType)(S3Settings.apply(s3Config)) + val result = headerFilter.map(header => (header.name(), header.value())) + result should contain allElementsOf (allowedHeaders.toSeq ++ Seq("allowedExtra" -> "allGood")) + } + + } + + it should "be able to convert all headers toString and back correctly" in { + val roundTrip = S3Request.allRequests + .map(_.toString()) + .flatMap(S3Request.fromString(_)) + + roundTrip should contain allElementsOf S3Request.allRequests + + } + + it should "contain all S3Request types" in { + // Get all known subclasses of the sealed trait using reflection + val allRequestTypes = getAllSealedTraitObjects[S3Request] + + // Your set to test + val actualSet = S3Request.allRequests.toSet + + // Verify they match + actualSet should contain theSameElementsAs allRequestTypes + + // Also verify the count + actualSet should have size allRequestTypes.size + } + + def getAllSealedTraitObjects[T: TypeTag]: Set[T] = { Review Comment: > I've used https://github.com/gzoller/scala-reflection in the past for getting the classes that implement a sealed trait (for Scala 3) - or my own fork of it, to be more precise Yeah I suggested https://github.com/zio/izumi-reflect because its cross compiled on Scala 2/3 so the source code is exactly the same > Again, I'm not sure it is worth the hassle here. Agreed -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
