All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.squirrelgrip.scientist4k.comparator.HeadersComparator.kt Maven / Gradle / Ivy

There is a newer version: 0.6.6
Show newest version
package com.github.squirrelgrip.scientist4k.comparator

import com.github.squirrelgrip.scientist4k.model.ComparisonResult
import com.github.squirrelgrip.scientist4k.model.ComparisonResult.Companion.SUCCESS
import com.github.squirrelgrip.scientist4k.model.ExperimentComparator
import com.github.squirrelgrip.scientist4k.model.ExperimentResponse
import com.google.common.collect.MapDifference
import com.google.common.collect.Maps
import com.google.common.net.HttpHeaders.*
import org.apache.http.Header

class HeadersComparator : ExperimentComparator {
    override fun invoke(control: ExperimentResponse, candidate: ExperimentResponse): ComparisonResult {
        val controlMap = map(control.headers)
        val candidateMap = map(candidate.headers)

        val diff: MapDifference = Maps.difference(controlMap, candidateMap)
        if (diff.areEqual()) {
            return SUCCESS
        }
        val entriesDiffering = diff.entriesDiffering().map { (headerName, headerValue) ->
            "Header[$headerName] value is different: ${headerValue.leftValue()} != ${headerValue.rightValue()}."
        }.toComparisonResult()
        val entriesOnlyInControl = diff.entriesOnlyOnLeft().keys.map {
            "Header[$it] is only in Control."
        }.toComparisonResult()
        val entriesOnlyInCandidate = diff.entriesOnlyOnRight().keys.map {
            "Header[$it] is only in Candidate."
        }.toComparisonResult()
        return ComparisonResult(entriesDiffering, entriesOnlyInControl, entriesOnlyInCandidate)
    }

    val IGNORED_HEADERS = arrayOf(
            SET_COOKIE,
            LAST_MODIFIED,
            DATE,
            CONTENT_LENGTH,
            CONTENT_TYPE
    )

    private fun map(control: Array
): Map { return (control).filter { it.name !in IGNORED_HEADERS }.map { it.name to it.value }.toMap() } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy