<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 使用 Restito 工具進行 REST 客戶端測試 > 原文: [https://www.guru99.com/rest-client-testing.html](https://www.guru99.com/rest-client-testing.html) ## 什么是 REST? **REST** 代表“代表狀態轉移”,這是在給定時間點任何兩個系統之間進行通信的新方式。 其中一個系統稱為“ REST 客戶端”,另一個系統稱為“ REST 服務器”。 在此 REST 教程中,您將學習: * [什么是 REST?](#1) * [什么是 REST 客戶端?](#2) * [什么是 REST Server?](#3) * [什么是 Restito?](#4) * [如何使用 Restito 測試 REST 客戶端?](#5) * [使用 Restito Framework 進行 REST 客戶端測試的優勢](#6) * [使用 Restito Framework 進行 REST 客戶端測試的缺點](#7) 在了解用于 REST 客戶端測試的 Restito Framework 之前,讓我們首先學習一些基礎知識。 ## 什么是 REST 客戶端? REST 客戶端是一種調用 REST 服務 API 的方法或工具,該 API 公開供任何系統或服務提供商進行通信。 例如:如果公開一個 API 來獲取來自 Google 的路線的實時路況信息,則調用 Google 流量 API 的軟件/工具稱為 REST 客戶端。 ## 什么是 REST Server? 它是任何系統或服務提供商都可以進行通信的方法或 API。 例如,Google 公開了一個 API,以獲取給定路線上的實時路況信息。 在這里,Google 服務器需要啟動并運行,以偵聽來自不同客戶端的對公開 API 的任何請求。 ### 例: 現在該根據上述定義建立一個完整的端到端方案。 讓我們考慮像 Uber 這樣的出租車預訂應用程序,因為公司需要有關給定車輛所在路線周圍交通狀況的實時信息。 ### 休息客戶: 客戶端是驅動程序已登錄的 Uber 移動應用程序。 該應用向 Google 地圖公開的 REST API 發送請求,以獲取實時數據。 例如,一個 HTTP GET 請求。 ### 休息服務器: 在此示例中,Google 是服務提供商,并且 Google Maps 的 API 會以所需的詳細信息響應 Uber 應用程序的請求。 客戶端和服務器在 REST 通信中同等重要。 在這里,我們實現了僅用于 REST 客戶端的自動化測試的示例。 要測試 REST 服務器,請參閱 [https://www.guru99.com/top-6-api-testing-tool.html](https://www.guru99.com/top-6-api-testing-tool.html) 。 ## 什么是 Restito? Restito 是 Mkotsur 開發的框架。 這是一個輕量級的應用程序,可以幫助您執行任何類型的 HTTP 請求。 您可以使用 Restito 測試 REST API 并搜索應用程序或網絡中的問題。 ## 如何使用 Restito 測試 REST 客戶端? 讓我們將練習分為以下四個步驟: 1. 創建一個 HTTP 客戶端和方法以將 HTTP GET 請求發送到任何服務器端點。 現在,將端點視為 [http:// localhost:9092 / getevents。](http://localhost:9092/getevents) 2. 啟動 Restito 服務器以偵聽并捕獲發送到 localhost [http:// localhost:9092 / getevents](http://localhost:9092/getevents) 中的端點“ getevents”的請求。 3. 創建一個測試類來測試上述客戶端。 調用 HTTP 客戶端的“ sendGETRequest”方法以向 API“ getevents”發起 GET 請求。 4. 使用 Restito 框架驗證 HTTP GET 調用。 讓我們深入研究上述每個步驟。 **步驟 1)**創建一個 HTTP 客戶端和方法,以將 HTTP GET 請求發送到任何服務器端點。 ========== Java 代碼開始=========== ``` package com.chamlabs.restfulservices.client; import java.util.HashMap; import java.util.Map; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.HttpClientBuilder; import org.json.JSONObject; /** * This class creates a HTTP Client and has a method to send HTTP GET request: * sendGETRequest(..) */ public class RestClient { /** * Constructor for the class RestClient */ public RestClient() { System.out.println("Creating RestClient constructor"); } /** * Method to Send GET request to http://localhost:<<port>>/getevents * @param port * @return true if GET request is successfully sent. False, otherwise. */ public static boolean sendGETRequest(int port) { try { HttpClient client = HttpClientBuilder.create().build(); HttpGet getRequest = new HttpGet("http://localhost:" + port + "/getevents"); //HttpResponse response = client.execute(request); client.execute(getRequest); System.out.println("HTTP request is sent successfully." + "Returning True"); return true; } catch (Exception e) { e.printStackTrace(); } System.out.println("Some exception has occurred during the HTTP Client creation." + "Returning false"); return false; } } ``` ========== JAVA CODE Ends =========== **步驟 2)**啟動 Restito 服務器以偵聽并捕獲發送到本地主機 [http:// localhost:9092 / getevents](http://localhost:9092/getevents) 中的端點“ getevents”的請求。 ========== JAVA CODE Starts =========== ``` package com.chamlabs.restfultesting.util; import static com.xebialabs.restito.builder.stub.StubHttp.whenHttp; import static com.xebialabs.restito.semantics.Action.status; import static com.xebialabs.restito.semantics.Condition.get; import static com.xebialabs.restito.semantics.Condition.post; import java.util.List; import org.glassfish.grizzly.http.util.HttpStatus; import com.xebialabs.restito.semantics.Call; import com.xebialabs.restito.server.StubServer; /** * This utility class contains several utility methods like : * restartRestitoServerForGETRequests(..) * restartRestitoServerForPOSTRequests(..) * waitAndGetCallList(..) * * @author cham6 * @email: paperplanes.chandra@gmail.com * @fork: https://github.com/cham6/restfultesting.git * */ public class TestUtil { /** * Utility method to start restito stub server to accept GET requests * @param server * @param port * @param status */ public static void restartRestitoServerForGETRequests (StubServer server, int port, HttpStatus status) { // Kill the restito server if (server != null) { server.stop(); } // Initialize and configure a newer instance of the stub server server = new StubServer(port).run(); whenHttp(server).match(get("/getevents")).then(status(status)); } /** * Utility method to start restito stub server to accept POST requests * @param server * @param port * @param status */ public static void restartRestitoServerForPOSTRequests (StubServer server, int port, HttpStatus status) { // Kill the restito server if (server != null) { server.stop(); } // Initialize and configure a newer instance of the stub server server = new StubServer(port).run(); whenHttp(server).match(post("/postevents")).then(status(status)); } /** * For a given restito stub server, loop for the given amount of seconds and * break and return the call list from server. * * @param server * @param waitTimeInSeconds * @return * @throws InterruptedException */ public static List<Call> waitAndGetCallList (StubServer server, int waitTimeInSeconds) throws InterruptedException { int timeoutCount = 0; List<Call> callList = server.getCalls(); while (callList.isEmpty()) { Thread.sleep(1000); timeoutCount++; if (timeoutCount >= waitTimeInSeconds) { break; } callList = server.getCalls(); } // Wait for 2 seconds to get all the calls into callList to Eliminate any falkyness. Thread.sleep(2000); return server.getCalls(); } } ``` ========== JAVA CODE Ends =========== **步驟 3)**創建一個測試類來測試上述客戶端。 調用 HTTP 客戶端的 sendGETRequest 方法,以啟動對 API“ getevents”的 GET 請求。 ========== JAVA CODE Starts =========== ``` import junit.framework.TestCase; import com.chamlabs.restfulservices.client.RestClient; import com.chamlabs.restfultesting.util.TestUtil; import com.xebialabs.restito.semantics.Call; import com.xebialabs.restito.server.StubServer; import static org.glassfish.grizzly.http.util.HttpStatus.ACCEPTED_202; import org.json.JSONObject; import java.util.List; import java.util.Map; /** * This class contains several junit tests to validate the RestClient operations like: * sendRequest(..) * sendRequestWithCustomHeaders(..) * sendPOSTRequestWithJSONBody(..) * */ public class RestClientTester extends TestCase { private static final Integer PORT = 9098; private static final Integer PORT2 = 9099; private static final Integer PORT3 = 9097; public RestClientTester() { System.out.println("Starting the test RestClientTester"); } /** * Junit test to validate the GET request from RestClient * Steps: * 1) Create a stub server using Restito framework and configure it to listen on given port * 2) Invoke the sendGETRequest(..) method of RestClient * 3) Restito captures the matching GET requests sent, if any. * 4) Validate if Restito has captured any GET requests on given endpoint * Expected Behavior: * > Restito should have captured GET request and it should have captured only one GET request. * Finally: * > Stop the stub server started using restito. */ public void testGETRequestFromClient() { StubServer server = null; try { //This will start the stub server on 'PORT' and responds with HTTP 202 'ACCEPTED_202' TestUtil.restartRestitoServerForGETRequests(server, PORT, ACCEPTED_202); RestClient.sendGETRequest(PORT); List<Call> callList = TestUtil.waitAndGetCallList(server, 30); assertTrue("GET request is not received from the RestClient. Test failed.", (callList != null) && (callList.size() == 1)); } catch(Exception e) { e.printStackTrace(); fail("Test Failed due to exception : " + e); } finally { if(server != null) { server.stop(); } } } ``` ========== JAVA CODE Ends =========== **步驟 4)**如何使用 Restito 框架驗證帶有標頭的 GET 請求和帶有正文的 POST 請求。 ========== JAVA CODE Starts =========== ``` /** * Junit test to validate the GET request with headers from RestClient * Steps: * 1) Create a stub server using Restito framework and configure it to listen on given port * 2) Invoke the sendGETRequestWithCustomHeaders(..) method of RestClient * 3) Restito captures the matching GET requests sent, if any. * 4) Validate if Restito has captured any GET requests on a given endpoint * Expected Behavior: * > Restito should have captured GET request, and it should have captured only one GET request. * > Get the headers of the captured GET request * and make sure the headers match to the ones configured. * Finally: * > Stop the stub server started using restito. */ public void testGETRequestWithHeadersFromClient() { StubServer server = null; try { //This will start the stub server on 'PORT' and responds with HTTP 202 'ACCEPTED_202' TestUtil.restartRestitoServerForGETRequests(server, PORT2, ACCEPTED_202); RestClient.sendGETRequestWithCustomHeaders(PORT2); List<Call> callList = TestUtil.waitAndGetCallList(server, 30); assertTrue("GET request is not received from the RestClient. Test failed.", (callList != null) && (callList.size() == 1)); //Validate the headers of the GET request from REST Client Map<String, List<String>> headersFromRequest = callList.get(0).getHeaders(); assertTrue("GET request contains header Accept and its value ", headersFromRequest.get("Accept").contains("text/html")); assertTrue("GET request contains header Authorization and its value ", headersFromRequest.get("Authorization").contains("Bearer 1234567890qwertyuiop")); assertTrue("GET request contains header Cache-Control and its value ", headersFromRequest.get("Cache-Control").contains("no-cache")); assertTrue("GET request contains header Connection and its value ", headersFromRequest.get("Connection").contains("keep-alive")); assertTrue("GET request contains header Content-Type and its value ", headersFromRequest.get("Content-Type").contains("application/json")); } catch(Exception e) { e.printStackTrace(); fail("Test Failed due to exception : " + e); } finally { if(server != null) { server.stop(); } } } ``` ``` /** * Junit test to validate the POST request with body and headers from RestClient * Steps: * 1) Create a stub server using Restito framework and configure it to listen on given port * 2) Invoke the sendPOSTRequestWithJSONBody(..) method of RestClient * 3) Restito captures the matching POST requests sent, if any. * 4) Validate if Restito has captured any POST requests on given endpoint * Expected Behavior: * > Restito should have captured POST request and it should have captured only one POST request. * > Get the body of the captured POST request and validate the JSON values * Finally: * > Stop the stub server started using restito. */ public void testPOSTRequestWithJSONBody() { StubServer server = null; try { //This will start the stub server on 'PORT' and responds with HTTP 202 'ACCEPTED_202' TestUtil.restartRestitoServerForPOSTRequests(server, PORT3, ACCEPTED_202); RestClient.sendPOSTRequestWithJSONBody(PORT3); List<Call> callList = TestUtil.waitAndGetCallList(server, 30); assertTrue("POST request is not received from the RestClient. Test failed.", (callList != null) && (callList.size() == 1)); //Validate the headers of the GET request from REST Client String requestBody = callList.get(0).getPostBody(); JSONObject postRequestJSON = new JSONObject(requestBody); assertTrue("The timeUpdated in json is incorrect", postRequestJSON.get("timeUpdated").toString().equalsIgnoreCase("1535703838478")); assertTrue("The access_token in json is incorrect", postRequestJSON.get("access_token").toString(). equalsIgnoreCase("abf8714d-73a3-42ab-9df8-d13fcb92a1d8")); assertTrue("The refresh_token in json is incorrect", postRequestJSON.get("refresh_token").toString(). equalsIgnoreCase("d5a5ab08-c200-421d-ad46-2e89c2f566f5")); assertTrue("The token_type in json is incorrect", postRequestJSON.get("token_type").toString().equalsIgnoreCase("bearer")); assertTrue("The expires_in in json is incorrect", postRequestJSON.get("expires_in").toString().equalsIgnoreCase("1024")); assertTrue("The scope in json is incorrect", postRequestJSON.get("scope").toString().equalsIgnoreCase("")); } catch(Exception e) { e.printStackTrace(); fail("Test Failed due to exception : " + e); } finally { if(server != null) { server.stop(); } } } } ``` ========== JAVA CODE Ends =========== ## 使用 Restito Framework 進行 REST 客戶端測試的優勢 這是用于 ReST 客戶端測試的 Restito Framework 的優點/優點 * 我們不需要開發實際的 REST 服務器來測試 REST 客戶端。 * Restito 提供了強大而多樣的實用程序和方法來模擬服務器的不同行為。 例如:測試服務器響應 HTTP 404 錯誤或 HTTP 503 錯誤時 REST 客戶端的行為。 * Restito 服務器可以在幾毫秒內完成設置,并且可以在測試完成后終止。 * Restito 支持所有類型的 HTTP 方法內容,例如壓縮,非壓縮,統一,應用程序/文本,應用程序/ JSON 等。 ## 使用 Restito Framework 進行 REST 客戶端測試的缺點 這是用于 ReST 客戶端測試的 Restito Framework 的缺點/缺點 * 應當調整 REST 客戶端源,以將“ localhost”視為服務器計算機。 * 如果我們使用一些常用端口(例如“ 8080”或“ 9443”等),則在任何端口打開服務器都可能會發生沖突。 * 建議使用其他工具不常用的端口,例如 9092 或 9099。 ### 摘要: * REST 代表“代表狀態轉移”,這是在給定時間點任何兩個系統之間進行通信的新標準方式。 * REST 客戶端是一種調用 REST 服務 API 的方法或工具,該 API 公開給任何系統或服務提供商進行通信。 * 在 RestServer 方法中或公開供任何系統或服務提供者進行通信的 API 中。 * Restito 是一款輕巧的應用程序,可幫助您執行任何類型的 HTTP 請求 * 創建一個 HTTP 客戶端和方法以將 HTTP GET 請求發送到任何服務器端點 * 啟動 Restito 服務器以偵聽和捕獲發送到端點“ getevents”的請求。 * 啟動 Restito 服務器以偵聽和捕獲發送到 localhost 中的端點“ getevents”的請求 * 在這里,我們實現了僅用于 REST 客戶端的自動化測試的示例。 * 我們不需要開發實際的 REST 服務器來測試 REST 客戶端。 * 應當調整 REST 客戶端源,以將“ localhost”視為服務器計算機。 本文由 Chandrasekhar Muttineni 提供
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看