Indranisgt
Indranisgt

Reputation: 1

How do I validate API specs using ReadyAPI?

I test apis using ReadyAPI. I am looking for a solution to verify the api spec. Sat we have a spec that has one resource , and it takes in two fields, field a is of length 3, field b is of length 4. Can we have a test step to consume resource and validate field a value is invalid in case length is provided as 4?

Can we write an utility in powershell that reads specification and produces a groovy snippet which we can reuse over and over to verify all our api specs?

Upvotes: 0

Views: 98

Answers (1)

chubbsondubs
chubbsondubs

Reputation: 38676

Why not just write a Groovy program that given a specification and an API endpoint it verifies that your API follows the specification? You can invoke that from powershell. Otherwise a powershell script that emits groovy snippet would still require invoking that snippet.

ReadyAPI seems to operate on OpenAPI specification so I'll stick to that.

If so, you could grab JsonSlurper to parse the specification if it's in JSON or grab YamlSlurper if it's YAML (Groovy 3.0+). After that it's just parsing that document looking for the paths, and generating API requests to it (Java 11 HttpClient or the older HttpBuilder from Groovy). It's not going to be simple, but it's not rocket science.

Here is an example of processing an OpenAPI spec file using Groovy to print out the title:

URL url = new URL("https://raw.githubusercontent.com/OAI/OpenAPI-Specification/main/examples/v3.1/non-oauth-scopes.json")
URLConnection conn = url.openConnection()
String charset = conn.getContentType().split(";")[1].split("=")[1].trim())
new BufferedReader( new InputStreamReader( conn.inputStream, charset ) ).withCloseable { buf ->
   String text = buf.readLines().join("\n")
   def json = new JsonSlurper().parseText( text )

   println( json.info.title )
   json.paths.each { path, v -> println( path ) }
}

Upvotes: 0

Related Questions