Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(server): migrate pascal case http headers rule to context object…
… to support openapi 3 (#714)
- Loading branch information
1 parent
9138b05
commit 3073b8f
Showing
3 changed files
with
42 additions
and
70 deletions.
There are no files selected for viewing
29 changes: 11 additions & 18 deletions
29
server/src/main/java/de/zalando/zally/rule/zalando/PascalCaseHttpHeadersRule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,24 @@ | ||
package de.zalando.zally.rule.zalando | ||
|
||
import com.typesafe.config.Config | ||
import de.zalando.zally.rule.HttpHeadersRule | ||
import de.zalando.zally.rule.api.Check | ||
import de.zalando.zally.rule.api.Context | ||
import de.zalando.zally.rule.api.Severity | ||
import de.zalando.zally.rule.api.Violation | ||
import de.zalando.zally.rule.api.Rule | ||
import de.zalando.zally.util.PatternUtil | ||
import io.swagger.models.Swagger | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import de.zalando.zally.util.getAllHeaders | ||
|
||
@Rule( | ||
ruleSet = ZalandoRuleSet::class, | ||
id = "132", | ||
severity = Severity.SHOULD, | ||
title = "Prefer Hyphenated-Pascal-Case for HTTP header fields" | ||
ruleSet = ZalandoRuleSet::class, | ||
id = "132", | ||
severity = Severity.SHOULD, | ||
title = "Prefer Hyphenated-Pascal-Case for HTTP header fields" | ||
) | ||
class PascalCaseHttpHeadersRule(@Autowired rulesConfig: Config) : HttpHeadersRule(rulesConfig) { | ||
class PascalCaseHttpHeadersRule { | ||
|
||
@Check(severity = Severity.SHOULD) | ||
override fun validate(swagger: Swagger): Violation? { | ||
return super.validate(swagger) | ||
} | ||
|
||
override fun isViolation(header: String) = !PatternUtil.isHyphenatedPascalCase(header) | ||
|
||
override fun createViolation(paths: List<String>): Violation { | ||
return Violation("Header is not Hyphenated-Pascal-Case", paths) | ||
} | ||
fun checkHttpHeaders(context: Context): List<Violation> = | ||
context.api.getAllHeaders() | ||
.filterNot { PatternUtil.isHyphenatedPascalCase(it.name) } | ||
.map { context.violation("Header has to be Hyphenated-Pascal-Case", it.element) } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 27 additions & 50 deletions
77
server/src/test/java/de/zalando/zally/rule/zalando/PascalCaseHttpHeadersRuleTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,45 @@ | ||
package de.zalando.zally.rule.zalando | ||
|
||
import de.zalando.zally.getFixture | ||
import de.zalando.zally.swaggerWithHeaderParams | ||
import de.zalando.zally.testConfig | ||
import io.swagger.models.Swagger | ||
import de.zalando.zally.getOpenApiContextFromContent | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.intellij.lang.annotations.Language | ||
import org.junit.Test | ||
|
||
class PascalCaseHttpHeadersRuleTest { | ||
|
||
private val rule = PascalCaseHttpHeadersRule(testConfig) | ||
private val rule = PascalCaseHttpHeadersRule() | ||
|
||
@Test | ||
fun simplePositiveCase() { | ||
val swagger = swaggerWithHeaderParams("Right-Name") | ||
assertThat(rule.validate(swagger)).isNull() | ||
} | ||
fun `checkHttpHeaders should return violation if not pascal case is used`() { | ||
@Language("YAML") | ||
val spec = """ | ||
openapi: 3.0.1 | ||
components: | ||
headers: | ||
not-pascal-case-header: {} | ||
""".trimIndent() | ||
val context = getOpenApiContextFromContent(spec) | ||
|
||
@Test | ||
fun simpleNegativeCase() { | ||
val swagger = swaggerWithHeaderParams("kebap-case-name") | ||
val result = rule.validate(swagger)!! | ||
assertThat(result.paths).hasSameElementsAs(listOf("parameters kebap-case-name")) | ||
} | ||
val violations = rule.checkHttpHeaders(context) | ||
|
||
@Test | ||
fun mustAcceptETag() { | ||
val swagger = swaggerWithHeaderParams("ETag") | ||
assertThat(rule.validate(swagger)).isNull() | ||
assertThat(violations).isNotEmpty | ||
assertThat(violations[0].description).isEqualTo("Header has to be Hyphenated-Pascal-Case") | ||
assertThat(violations[0].pointer.toString()).isEqualTo("/components/headers/not-pascal-case-header") | ||
} | ||
|
||
@Test | ||
fun mustAcceptZalandoHeaders() { | ||
val swagger = swaggerWithHeaderParams("X-Flow-ID", "X-UID", "X-Tenant-ID", "X-Sales-Channel", "X-Frontend-Type", | ||
"X-Device-Type", "X-Device-OS", "X-App-Domain") | ||
assertThat(rule.validate(swagger)).isNull() | ||
} | ||
fun `checkHttpHeaders should return no violation if all headers are pascal case`() { | ||
@Language("YAML") | ||
val spec = """ | ||
openapi: 3.0.1 | ||
components: | ||
headers: | ||
Hyphenated-Pascal-Case-Header: {} | ||
""".trimIndent() | ||
val context = getOpenApiContextFromContent(spec) | ||
|
||
@Test | ||
fun mustAcceptRateLimitHeaders() { | ||
val swagger = swaggerWithHeaderParams("X-RateLimit-Limit", "X-RateLimit-Remaining", "X-RateLimit-Reset") | ||
assertThat(rule.validate(swagger)).isNull() | ||
} | ||
val violations = rule.checkHttpHeaders(context) | ||
|
||
@Test | ||
fun mustAcceptDigits() { | ||
val swagger = swaggerWithHeaderParams("X-P1n-Id") | ||
assertThat(rule.validate(swagger)).isNull() | ||
} | ||
|
||
@Test | ||
fun emptySwaggerShouldPass() { | ||
assertThat(rule.validate(Swagger())).isNull() | ||
} | ||
|
||
@Test | ||
fun positiveCaseSpp() { | ||
val swagger = getFixture("api_spp.json") | ||
assertThat(rule.validate(swagger)).isNull() | ||
} | ||
|
||
@Test | ||
fun positiveCaseTinbox() { | ||
val swagger = getFixture("api_tinbox.yaml") | ||
assertThat(rule.validate(swagger)).isNull() | ||
assertThat(violations).isEmpty() | ||
} | ||
} |