当前位置: 动力学知识库 > 问答 > 编程问答 >

groovy - Spock: check the query parameter count in URI

问题描述:

I have just started with spock. I have one functionality. where the java function makes an http call. As per functionality, the URI used in http call, must contain "loc" parameter and it should be only once.

I am writing Spock test case. I have written below snippet.

def "prepareURI" () {

given: "Search Object"

URI uri = new URI();

when:

uri = handler.prepareURI( properties) // it will return URI like http://example.com?query=abc&loc=US

then:

with(uri)

{

def map = uri.getQuery().split('&').inject([:]) {map, kv-> def (key, value) = kv.split('=').toList(); map[key] = value != null ? URLDecoder.decode(value) : null; map }

assert map.loc != null

}

}

From above snippet, my 2 tests got passed like

  • It should be exists
  • It should not be null

I want to check the count of "loc" query parameter. It should be passed exactly once. With map as above, If I pass "loc" parameter twice, map overrides the old value with 2nd one.

Does any one knows, how to access the query parameters as list, and in list I want to search the count of Strings which starts with "loc"

Thanks in advance.

网友答案:

Perhaps an example would be the best start:

def uri    = new URI('http://example.com?query=abc&loc=US')
def parsed = uri.query.tokenize('&').collect { it.tokenize('=') }

println "parsed to list: $parsed"
println "count of 'loc' params:            " + parsed.count { it.first() == 'loc' }
println "count of 'bob' params:            " + parsed.count { it.first() == 'bob' }
println "count of params with value 'abc': " + parsed.count { it.last()  == 'abc' }

prints:

$ groovy test.groovy 
parsed to list: [[query, abc], [loc, US]]
count of 'loc' params:            1
count of 'bob' params:            0
count of params with value 'abc': 1

the problem, as you correctly noted, is that you can not put your params into a map if your intent is to count the number of params with a certain name.

In the above, we parse the params in to a list of lists where the inner lists are key, value pairs. This way we can call it.first() to get the param names and it.last() to get the param values. The groovy List.count { } method lets us count the occurences of a certain item in the list of params.

As for your code, there is no need to call new URI() at the beginning of your test as you set the value anyway a few lines down.

Also the with(uri) call is unnecessary as you don't use any of the uri methods without prefixing them with uri. anyway. I.e. you can either write:

def uri    = new URI('http://example.com?query=abc&loc=US')
def parsed = uri.query.tokenize('&').collect { it.tokenize('=') }

or:

def uri = new URI('http://example.com?query=abc&loc=US')
uri.with { 
  def parsed = query.tokenize('&').collect { it.tokenize('=') }
}

(note that we are using query directly in the second example)

but there is not much point in using with if you are still prefixing with uri..

The resulting test case might look something like:

def "prepareURI"() {
    given: "Search Object"
        def uri = handler.prepareURI( properties) // it will return URI like http://example.com?query=abc&loc=US

    when:
        def parsed = query.tokenize('&').collect { it.tokenize('=') }

    then:
        assert parsed.count { it.first() == 'loc' } == 1
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: