标签: web-api-testing

集成测试 multipart/form-data c#

我在尝试为我的后调用创建一个集成测试时遇到了麻烦,该测试接受一个具有其他值的视图模型,一个 IFormFile,它使此调用从 application/json 到 multipart/form-data

我的 IntegrationSetup 类

protected static IFormFile GetFormFile()
        {
            byte[] bytes = Encoding.UTF8.GetBytes("test;test;");

            var file = new FormFile(
                baseStream: new MemoryStream(bytes),
                baseStreamOffset: 0,
                length: bytes.Length,
                name: "Data",
                fileName: "dummy.csv"
            )
            {
                Headers = new HeaderDictionary(),
                ContentType = "text/csv"
            };

            return file;
        } 
Run Code Online (Sandbox Code Playgroud)

我的测试方法

public async Task CreateAsync_ShouldReturnId()
        {
            //Arrange
            using var content = new MultipartFormDataContent();
            var stringContent = new StringContent(
                JsonConvert.SerializeObject(new CreateArticleViewmodel
                {
                    Title = "viewModel.Title",
                    SmallParagraph = "viewModel.SmallParagraph",
                    Url = "viewModel.Url",
                    Image = GetFormFile()
                }), …
Run Code Online (Sandbox Code Playgroud)

c# integration-testing web-api-testing asp.net-core asp.net-core-webapi

2
推荐指数
1
解决办法
2822
查看次数

使用代码接收测试文件上传

问题:

当使用带有PhpBrowser驱动程序的REST模块通过代码接收测试发出请求时,没有数据和文件通过Silex应用程序。

    // ApiTester $I
    $I->wantTo('Submit files');

    // prepare:
    $data = ['key' => 'value'];
    $files = [
        'file_key' => 'path/to/file.doc',
        'file2_key' => 'path/to/file2.doc'
    ];

    // act:
    $I->haveHttpHeader('Content-Type', 'multipart/form-data');
    $I->sendPOST('/attachments/', $data, $files);
Run Code Online (Sandbox Code Playgroud)

当前反应

 I have http header "Content-Type","multipart/form-data"
 I send post "/attachments/",{"key":"value"},{"file_key":"/path/to/file/...}
  [Request] POST http://localhost/attachments/ {"key":"value"}
  [Request Headers] {"Content-Type":"multipart/form-data"}
  [Page] http://localhost/attachments/
  [Response] 400
  [Request Cookies] []
  [Response Headers] {"Date":["Tue, 25 Oct 2016 09:15:31 GMT"],"Server":["Apache/2.4.10 (Debian)"],"Cache-Control":["no-cache"],"Access-Control-Allow-Origin":["*"],"Access-Control-Allow-Headers":["Content-Type, Authorization"],"Access-Control-Allow-Methods":["GET,PATCH,PUT,POST,HEAD,DELETE,OPTIONS"],"Content-Length":["1235"],"Connection":["close"],"Content-Type":["application/json"]}
  [Response] {"status":400,"meta":{"time":"2016-10-25 09:15:31"},"title":"Invalid Request","errors":"No data received","details":{"error_class":"Symfony\Component\HttpKernel\Exception\BadRequestHttpException"
Run Code Online (Sandbox Code Playgroud)

尝试过:

  • 更改Content-Type标头
  • 更改传递给sendPOST的文件数组到以下数组:
    • 文件路径文件对象(UploadedFile)
    • 文件数组

该测试可与Silex驱动程序一起使用,但在CI服务器上将不可选。我们还检查了Postman,API路由按预期工作,文件已发送且一切正常。

phpunit codeception web-api-testing

1
推荐指数
1
解决办法
1913
查看次数

java.lang.NoClassDefFoundError: io/restassured/path/json/mapper/factory/JsonbObjectMapperFactory

使用放心的 jar 发出 GET api 请求时,我的程序在作为 TestNg 运行时抛出以下错误

输出:

java.lang.NoClassDefFoundError: io/restassured/path/json/mapper/factory/JsonbObjectMapperFactory
    at io.restassured.config.RestAssuredConfig.<init>(RestAssuredConfig.java:41)
    at io.restassured.RestAssured.<clinit>(RestAssured.java:421)
    at SimpleGetTest.GetWeatherDetails(SimpleGetTest.java:13)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:583)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
    at org.testng.TestRunner.privateRun(TestRunner.java:648)
    at org.testng.TestRunner.run(TestRunner.java:505)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
    at org.testng.SuiteRunner.run(SuiteRunner.java:364)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
    at org.testng.TestNG.runSuites(TestNG.java:1049)
    at org.testng.TestNG.run(TestNG.java:1017)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
Caused by: java.lang.ClassNotFoundException: io.restassured.path.json.mapper.factory.JsonbObjectMapperFactory
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown …
Run Code Online (Sandbox Code Playgroud)

java testng jar rest-assured web-api-testing

1
推荐指数
1
解决办法
2930
查看次数

RestSharp JSON POST 请求遇到错误请求

我正在使用 RestSharp 发出包含 JSON 正文的 POST 请求。但我收到错误请求错误。

因为我已经[]""J​​SON 中决定使用 Newtonsoft.Json 。在使用它之前,我什至看不到正在形成的 JSON 请求。

我愿意尝试MS httpwebrequest作为替代方案。

restClient = new RestClient();

restRequest = new RestRequest(ApiUrl, Method.POST, DataFormat.Json);

var myObject = "{ \"target\" : \"[5,5]\", \"lastseen\" : \"1555459984\" }";

var json = JsonConvert.SerializeObject(myObject);
restRequest.AddParameter("application/json", ParameterType.RequestBody);

restRequest.AddJsonBody(json);
Run Code Online (Sandbox Code Playgroud)

请注意,我正在尝试将 JSON 卷曲转换为 C#。请看下面:

curl -H 'Content-Type: application/json' -X POST -d '{ "target" : [5, 5], "lastseen" : "1555459984", "previousTargets" : [ [1, 0], [2, 2], [2, 3] ] }' http://santized/santized/santized

c# post json restsharp web-api-testing

0
推荐指数
1
解决办法
9754
查看次数

使用 cypress 进行 API 测试时,出现“expected { Object (message, detail) } to have property 'count'”错误

我想断言从响应中收到的总数。

这是我的代码:

cy.request({
        method:'GET',
        url:'https://ibis-qa.droicelabs.us/api/practice/orders/?q=&limit=100',
        failOnStatusCode: false,
        headers:{
            accept: "application/json"
        }
    }).then(Response => {
        let body = JSON.parse(JSON.stringify(Response.body))
        cy.log(body)
        expect(body).has.property('count','27')
   })
Run Code Online (Sandbox Code Playgroud)

这是我遇到的错误

在此输入图像描述

响应体

rest web-api-testing cypress

0
推荐指数
1
解决办法
2439
查看次数

我想使用 cypress 中的 cy.intercept 从 API 响应中获取 OrderID

我想从 API 响应中获取订单 ID。当我单击“创建订单”按钮时,它将发送 POST API 请求并返回我想要保存在 JSON 文件中的 ID。

这是我的订单创建代码。

cy.clickOnElement(practicePageSelectors.CreateOrder).click(); // click on add Rx button
cy.readFile('cypress/fixtures/Data.json').then((profile) => {
   cy.searchPatients(practicePageSelectors.searchPatient1, profile.Patient_fullName);
})
cy.searchDoctors(); // search for the doctor
cy.clickOnElementUsingXpath(practicePageSelectors.nextButtonId); // click on the next button
cy.clickOnElement(practicePageSelectors.createOnetimeOrder)
cy.searchMedicine() //search for Medicine
cy.clickOnElementUsingXpathfirst(practicePageSelectors.addMedicine); // click on add button
cy.clickOnElementUsingText(practiceData.paymentButtonName, practiceData.buttonTag); // click on skip payment button
cy.clickOnElementUsingXpath(practicePageSelectors.submit_CreateOrderButton)
Run Code Online (Sandbox Code Playgroud)

我尝试过这样的事情

cy.intercept({
      method: 'POST',
      url: 'https://ibis-dev.droicelabs.us/api/dispenser/orders/',
    }).then((responce)=>{
      let body = JSON.parse(responce.body)
      cy.log(body)
    })
Run Code Online (Sandbox Code Playgroud)

我不知道如何使用intercept。请指导我

automated-tests web-api-testing cypress

0
推荐指数
1
解决办法
682
查看次数

Cypress:有什么方法可以用测试用例中生成的数据替换固定字段以生成动态有效负载(尝试了现有的 sol)

我是赛普拉斯的新手。我想知道是否有任何方法可以通过用 cypress 测试中以编程方式生成的值替换 JSON 文件的值来生成动态有效负载。我们在放心中所做的事情是替换 JSON 文件的 %s。我在网上搜索了很多但没有找到。类似的一些问题是这对我不起作用 我想将动态 json 正文传递给 cypress request() 函数并定义有效负载值

我有以下 json 文件

{
    "name": "Ganesh vishal kumar",
    "gender": "Male",
    "email": "ganesh.kumar234@test.org",
    "status": "active"
}
Run Code Online (Sandbox Code Playgroud)

我想要一个动态 JSON 文件装置。在下面的测试中,我以编程方式生成电子邮件 ID 并直接在 JSON 正文中使用它。这里我想使用JSON文件fixture

it('first post test', () => {
        var pattern = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        var emailId = "";

        for (var x = 0; x < 10; x++) {
            emailId = emailId + pattern.charAt(Math.floor(Math.random() * pattern.length));
        }
        emailId = emailId + '@test.org'
        cy.request({
            method: 'POST',
            url: 'https://gorest.co.in/public/v2/users',
            body: { …
Run Code Online (Sandbox Code Playgroud)

json typescript web-api-testing cypress

-2
推荐指数
1
解决办法
347
查看次数