findify / s3mock

Embedded S3 server for easy mocking
MIT License
387 stars 107 forks source link

last-modified always equals to current date #139

Open antonkw opened 5 years ago

antonkw commented 5 years ago

Currently LAST_MODIFIED always equals to now() due to this string: objectMetadata.setLastModified(org.joda.time.DateTime.now().toDate) It blocks any attempt to test Date-related testing. Fix for it is pretty simple:

 override def putObject(bucket: String, key: String, data: Array[Byte], objectMetadata: ObjectMetadata): Unit = {
    bucketDataStore.get(bucket) match {
      case Some(bucketContent) =>
        logger.debug(s"putting object for s3://$bucket/$key, bytes = ${data.length}")
        Option(objectMetadata.getRawMetadataValue(Headers.LAST_MODIFIED)) match {
          case Some(date) if date.isInstanceOf[Date] => bucketContent.keysInBucket.put(key,
            KeyContents(DateTime(date.asInstanceOf[Date].getTime), data))
          case None =>
            bucketContent.keysInBucket.put(key, KeyContents(DateTime.now, data))
            objectMetadata.setLastModified(org.joda.time.DateTime.now().toDate)
        }
        metadataStore.put(bucket, key, objectMetadata)
      case None => throw NoSuchBucketException(bucket)
    }
  }

I am going to create PR for it if there are no additional suggestions.

Thanks.