2015-09-09 15 views
6

qualcuno può aiutarmi in questo scenario:come passare parametri a riposare sicuri

Quando invoco questo servizio, http://restcountries.eu/rest/v1/, ottengo paio di informazioni paesi.

Tuttavia, quando desidero ottenere informazioni specifiche sul paese come ad esempio la Finlandia, invoco il servizio Web come http://restcountries.eu/rest/v1/name/Finland per ottenere le informazioni relative al paese.

Per automatizzare lo scenario precedente, come è possibile parametrizzare il nome del paese in Rest-Assured? Ho provato di seguito, ma non mi aiuta.

RestAssured.given(). 
        parameters("name","Finland"). 
      when(). 
        get("http://restcountries.eu/rest/v1/"). 
      then(). 
       body("capital", containsString("Helsinki")); 

risposta

0

Si sta invocando la chiamata GET in modo errato.

Il parameters("name","Finland") saranno convertiti solo come interrogazione parametro o forma parametro per GET e POST/PUT rispettivamente

RestAssured.when(). 
        get("http://restcountries.eu/rest/v1/name/Finland"). 
      then(). 
       body("capital", containsString("Helsinki")); 

è l'unico modo per farlo. Dal momento che è un DSL Java, è possibile costruire l'URL tutto da solo e passarlo a get() se richiesto

Se l'URL con una richiesta GET ha dovuto prendere gli stessi dettagli stato così:

http://restcountries.eu/rest/v1?name=Finland,

La vostra DSL sarebbe qualcosa di simile:

RestAssured.given().parameters("name","Finland"). 
       when(). 
         get("http://restcountries.eu/rest/v1") 

Quando si dispone di una richiesta GET, i parametri si trasformano in queryParameters.

Maggiori informazioni da questo link: https://code.google.com/p/rest-assured/wiki/Usage#Parameters

+0

Sembra che la chiamata al servizio Web non sia corretta, quando invoco, http://restcountries.eu/rest/v1?name=Finland, sto ricevendo le informazioni di tutti i paesi, piuttosto se provi "http://restcountries.eu/rest/v1/name/Finland "stai ricevendo informazioni specifiche sul paese. – Uday

+0

La mia risposta è già stata corretta. pls check –

+0

Se passiamo parametro per valore, allora qual è il vantaggio di avere parametri qui? \t String strCountryName = "Finland"; \t \t RestAssured.given(). // \t \t \t \t \t \t parametri \t ("nome", "Finlandia"). \t \t \t \t \t quando(). \t \t \t \t \t \t \t get ("http://restcountries.eu/rest/v1/name/" + strCountryName). \t \t \t \t \t quindi(). \t \t \t \t \t \t corpo ("capitale", hasItem ("Helsinki")); Perché ho bisogno dei parametri sopra i quali ho commentato? – Uday

9

Come la documentazione spiega:

stare certi cercherà automaticamente di determinare quale tipo di parametro (cioè query o parametro di modulo) basato sul metodo HTTP . In caso di i parametri di query GET verranno automaticamente utilizzati e in caso di POST verranno utilizzati i parametri del modulo.

Ma nel tuo caso sembra che tu abbia bisogno di parametri di percorso invece di parametri di interrogazione. Nota inoltre che l'URL generico per ottenere un Paese è http://restcountries.eu/rest/v1/name/{country} dove {country} è il nome del paese.

Quindi, ci sono anche più modi per trasferire i parametri del percorso.

Ecco alcuni esempi

Esempio con pathParam():

// Here the key name 'country' must match the url parameter {country} 
RestAssured.given() 
     .pathParam("country", "Finland") 
     .when() 
      .get("http://restcountries.eu/rest/v1/name/{country}") 
     .then() 
      .body("capital", containsString("Helsinki")); 

Esempio con variabile:

String cty = "Finland"; 

// Here the name of the variable have no relation with the URL parameter {country} 
RestAssured.given() 
     .when() 
      .get("http://restcountries.eu/rest/v1/name/{country}", cty) 
     .then() 
      .body("capital", containsString("Helsinki")); 

Ora, se avete bisogno di chiamare i servizi diversi, è anche possibile parametrizzare il "servizio" come questo:

// Search by name 
String val = "Finland"; 
String svc = "name"; 

RestAssured.given() 
     .when() 
      .get("http://restcountries.eu/rest/v1/{service}/{value}", svc, val) 
     .then() 
      .body("capital", containsString("Helsinki")); 


// Search by ISO code (alpha) 
val = "CH" 
svc = "alpha" 

RestAssured.given() 
     .when() 
      .get("http://restcountries.eu/rest/v1/{service}/{value}", svc, val) 
     .then() 
      .body("capital", containsString("Bern")); 

// Search by phone intl code (callingcode) 
val = "359" 
svc = "callingcode" 

RestAssured.given() 
     .when() 
      .get("http://restcountries.eu/rest/v1/{service}/{value}", svc, val) 
     .then() 
      .body("capital", containsString("Sofia")); 

Dopo aver anche facilmente utilizzato JUnit @RunWith(Parameterized.class) per alimentare i parametri "svc" e "valore" per un test di unità.

+1

Questo è un modo così elegante di parametrizzare i valori del percorso! Proprio come indicato dall'API RequestSpecification, questo approccio migliora effettivamente la leggibilità e consente il riutilizzo dei percorsi! http://static.javadoc.io/com.jayway.restassured/rest-assured/1.2.3/com/jayway/restassured/specification/RequestSpecification.html#pathParameter(java.lang.String,%20java.lang. Oggetto) –