ProxymanApp / proxyman-windows-linux

Public tracker for Proxyman Windows/Linux
https://proxyman.io/
MIT License
107 stars 4 forks source link

[Map Local] #5 Unit Tests #209

Open NghiaTranUIT opened 1 year ago

NghiaTranUIT commented 1 year ago

Accepept Criteria

⚠️ Make sure all Unit Tests are implemented. I will carefully check it 👍 ⚠️ Please use the correct JSON data. You can capture it from httpbin.org or producthunt.com. Don't create a fake Unit Test Data, for example:

HTTP/1.1 200 OK
asdsa: asdsad
ASdsad: adsad

{":sdasd": 132}
  1. Start the Proxy Server
  2. Create a flow
  3. Create a Map Local from a flow
  4. At this step, we can try to change the Status Code, header or the body of the Raw Message of the Map Local
  5. Make a real request
  6. Assert if the body is exactly the Response that we've updated ✅

Reference Unit Test:

    func testMapLocalFile_MapDefaultFile() throws {
        let jsonExpected = "{\n  \"status\": \"ok\"\n}"

        // Start the Proxyman Proxy Server
        core = PMProxyServer.allwaysInterceptCore()
        runProxyCore { (expectation) in

            // Create a flow
            let url = "https://httpbin.org/post"
            let body: [String: Any] = ["name": "Original Proxyman",
                                       "country": 123,
                                       "code": "VN"]
            let flow = FlowFactory.flowWithJSON()

            // Create a Map Local Rule from a given Flow
            let entry = try! MapLocalFactory.buildMapLocal(flow: flow)

            // insert it
            MapLocalService.shared.insert(entry: entry, atNode: nil)

            // Use Networking library to make a call
            self.manager.request(url, method: .post, parameters: body, encoding: JSONEncoding(), headers: nil).responseJSON { response in
                let header = response.response?.allHeaderFields["X-Proxyman-Map-Local"] as? String
                XCTAssertNotNil(header)
                XCTAssertTrue(header!.hasPrefix("Serve from file:"))

                // Verify the JSON
                XCTAssertEqual(jsonExpected, String(data: response.data!, encoding: .utf8))
                expectation.fulfill()
            }
        }
    }

Unit Test for MapLocalService

1. Map with the default HTTP Message file

2. Map Local from a given flow

3. Map Local from a given flow - by selecting a local HTTP Message file

4. Map Local from a given flow - by selecting a local JSON File

5. Map Local from a given flow - by selecting a local Image PNG File (Binary)

6. Map Local, but not matching

7. Map Local from a given flow with GraphQL

78 Map Local from a given flow with GraphQL, but different graphQL Query Name

NghiaTranUIT commented 1 year ago

WildCardRegexTests Unit Tests