如何从放心请求XML内容类型

时间:2015-04-10 12:36:49

标签: xml rest junit rest-assured

我有一个支持json和xml的REST api。我想测试XML方面,但自从升级到2.4.0版本后,我收到了一个错误:

预期的内容类型" XML"与实际内容类型" application / json"

不匹配
        <dependency>
            <groupId>com.jayway.restassured</groupId>
            <artifactId>rest-assured</artifactId>
            <version>2.4.0</version>
        </dependency>
        <dependency>
            <groupId>com.jayway.restassured</groupId>
            <artifactId>json-path</artifactId>
            <version>2.4.0</version>
        </dependency>
        <dependency>
            <groupId>com.jayway.restassured</groupId>
            <artifactId>xml-path</artifactId>
            <version>2.4.0</version>
        </dependency>

这里有一些未通过测试的测试代码示例。

    RequestSpecification requestSpec = new RequestSpecBuilder()
            .setContentType("application/xml")
            .build();

    expect()
            .contentType(ContentType.XML)
            .statusCode(200)
            .given()
            .spec(requestSpec)
            .get("/resources/main/dictionaries");

我还尝试直接在给定()

上设置内容类型
    given()
            .contentType("application/xml")
            .expect()
            .contentType(ContentType.XML)
            .statusCode(404)
            .get("/resources/main/revisions/10000");

如何强制请求获取XML媒体类型?

请求日志如下所示:

Request method: GET
Multiparts:             <none>
Headers:                Accept=*/*
                            Content-Type=application/xml; charset=ISO-8859-1
Body:                   <none>

1 个答案:

答案 0 :(得分:1)

您需要Accept标题:

given()
       .spec(requestSpec)
       .header("Accept", "application/xml")
       .get("/resources/main/revisions/10000")