2016-09-22 42 views
0

請考慮下面的代碼,休息保證的JIRA的Java無法得到響應

import org.junit.Test; 
import org.omg.CORBA.Request; 

import com.jayway.restassured.RestAssured; 
import com.jayway.restassured.response.Response; 

import static com.jayway.restassured.RestAssured.*; 
import static org.hamcrest.Matchers.*; 

public class App 
{ 

    @Test 
    public void loginAuthentication() 
    { 

     RestAssured.authentication = basic("username", "password"); 

     Response resp = given(). 
      contentType("application/x-www-form-urlencoded"). 
     when(). 
      post("https://unitrends.atlassian.net/login?username=Gayatri.londhe&password=GayatriA4"); 

     System.out.println(resp.prettyPrint()); 
    } 
} 

我得到如下回應,

<div class="aui-message error"> 
    <span class="aui-icon icon-error"/> 
    <span class="error" id="error-authentication_failure_invalid_credentials"> 
     Sorry, we didn't recognize that username and password combination. Please double-check and try again. 
    </span> 
</div> 

如何使用驗證休息,放心,我我可以使用REST控制檯(chrome插件)做休息發佈請求。 我在做什麼錯?

我希望能在這方面得到一些幫助。我是新來的放心測試。

回答

0

嘗試......

Response resp = given(). 
      contentType("application/x-www-form-urlencoded").expect().response().statusCode(200). 
      when(). 
      post("https://unitrends.atlassian.net/login?username=Gayatri.londhe&password=GayatriA4"); 

如果響應狀態是200.Change它將會獲取響應,如果你的服務端點比200成功返回等。

試試這個

@BeforeClass 
public void setup() throws org.json.JSONException, IOException { 
    //Assigning base url 
    RestAssured.baseURI = "https://unitrends.atlassian.net/"; 
    //Assigning default port 
    RestAssured.port = <port> 
    RestAssured.basePath = "login"; 
    RestAssured.config = config().redirect(RedirectConfig.redirectConfig().followRedirects(true).and().allowCircularRedirects(true).maxRedirects(100)); 
} 
@Test 
public void testRestServiceAuthentication() { 
    //for a get method with basic authorization,its expecting response code 200 
    Response res = given().expect().response().statusCode(200).when().post("?username=Gayatri.londhe&password=GayatriA4"); 
    System.out.println(res.getBody().asString()); 
} 
+0

測試運行成功, 我想從響應中提取atl_Token。我怎樣才能做到這一點? –

+0

以及爲什麼我沒有看到這個api打到小提琴手? –

+0

'String output = response.getBody()。asString(); System.out.println(輸出); JSONObject obj = new JSONObject(output); String status = String.valueOf(obj.get(「」)); } ' 你可以使用這個..這個令牌是作爲頭髮送? –