REST API测试用例失败,并且在POSTMAN中传递了相同的内容

时间:2019-02-12 16:33:34

标签: java rest junit postman

已经发布了POSTMAN的图片以及REST API测试用例和响应输出。无法弄清楚测试用例失败的原因。感谢任何人的帮助/建议。

在Postman工具中尝试过,效果很好

REST API测试用例:

public class GetStripeTokenTest {
    public static String id_token;
    RequestSpecification spec;

    Properties prop = new Properties();

    @BeforeTest
    public void getData() throws IOException, InterruptedException {

    FileInputStream file = new FileInputStream(FilePath.PROPERTY_FILE_PATH);
        prop.load(file);
        spec = new RequestSpecBuilder()
        .setContentType(ContentType.URLENC)
        .setBaseUri(prop.getProperty("StripeURI"))
        .addFilter(new ResponseLoggingFilter())
        .addFilter(new RequestLoggingFilter())
        .build();
        Thread.sleep(5000);
    }

    @Test(priority=1)
    public void getToken() {
        Response resp = (Response)
        //given().spec(spec).headers("Authorization","Bearer " + GetStripeKeyTest.token).body(payload.getStripeToken()).when().post("/v1/tokens").then().assertThat().statusCode(400)
        given().spec(spec).headers("Authorization","Bearer " + "pk_test_p8TiG5oaBQ1tcQr7jo1vYJxa").body(payload.getStripeToken()).when().post("/v1/tokens").then().assertThat().statusCode(200)
        .extract().response();

        JsonPath js = response.rawToJson(resp);
        //Grabbing the Token that is generated under US from the response and storing in variable token
        id_token = js.get("id");
        System.out.println("Token is " + id_token);
    }
}

有效载荷的主体

public static String getStripeToken() {
    String strtok = "{" +
        "\"card\": "+
        "{" +
        "\"number\": 4242424242424242 ,"+
        "\"exp_month\" : 05 ,"+
        "\"exp_year\": 21 ,"+
        "\"cvc\" : 523 ,"+
        "\"address_line1\" : \" Renner Road \" ,"+
        "\"address_state\" : \"TX \" ,"+
        "\"address_zip\" : \"75023\" ,"+
        "\"address_country\" : \"United States \" "+
        "}" +
    "}" ;
        return strtok;
}

运行API测试用例后的响应:

Request method: POST
Request URI:    https://api.stripe.com/v1/tokens
Proxy:  <none>
Request params: <none>
Query params:   <none>
Form params:    <none>
Path params:    <none>
Headers:    Authorization=Bearer pk_test_p8TiG5oaBQ1tcQr7jo1vYJxa
Accept=*/*
Content-Type=application/x-www-form-urlencoded; charset=ISO-8859-1
Cookies:    <none>
Multiparts: <none>
Body:
{"card": {"number": 4242424242424242 ,"exp_month" : 05 ,"exp_year": 21 ,"cvc" : 523 ,"address_line1" : " Renner Road " ,"address_state" : "TX " ,"address_zip" : "75023" ,"address_country" : "United States " }}
HTTP/1.1 400 Bad Request
Server: nginx
Date: Thu, 07 Feb 2019 20:43:44 GMT
Content-Type: application/json
Content-Length: 739
Connection: keep-alive
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: GET, POST, HEAD, OPTIONS, DELETE
Access-Control-Allow-Origin: *
Access-Control-Expose-Headers: Request-Id, Stripe-Manage-Version, X-Stripe-External-Auth-Required, X-Stripe-Privileged-Session-Required
Access-Control-Max-Age: 300
Cache-Control: no-cache, no-store
Request-Id: req_ib4puv4FvepQIL
Stripe-Version: 2018-07-27
Strict-Transport-Security: max-age=31556926; includeSubDomains; preload
{

    "error": {

        "code": "parameter_missing",

        "doc_url": "https://stripe.com/docs/error-codes/parameter-missing",

        "message": "You must supply either a card, customer, PII data, bank account, or account legal entity to create a token. If you're making this request with a library, be sure to pass all of the required parameters for creating a token. If you're making this request manually, be sure your POST parameters begin with the token type. For example, a PII token would require `pii[personal_id_number]`, while an account token would require a parameter beginning with `account[legal_entity]`. See the API reference for more information: https://stripe.com/docs/api#token_object",

        "type": "invalid_request_error"

    }

}

See screenshot

0 个答案:

没有答案