[GH-ISSUE #209] [Map Local] #5 Unit Tests #210

Open
opened 2026-03-03 19:49:11 +03:00 by kerem · 1 comment
Owner

Originally created by @NghiaTranUIT on GitHub (Mar 5, 2023).
Original GitHub issue: https://github.com/ProxymanApp/proxyman-windows-linux/issues/209

Originally assigned to: @kics223w1 on GitHub.

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}
  • Unit Test Strategy
  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

  • Input: Create a Map Local with a default HTTP
  • Expected: A flow is matched, and the Response Body is the same as the default HTTP Raw Message

2. Map Local from a given flow

  • Input: Create a Map Local with a given JSON Flow -> Modify the status code, add some header, and change some body
  • Expected: A flow is matched, and the Response Body is the same as we've updated (Status code, Header, Body)

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

  • Input:
  1. Create a Map Local with a given JSON Flow
  2. Create an HTTP Message with some Status code, Header, and JSON body -> Save it to the temporary folder for testing
  3. Ask the Map Entry to select a local file (created in the 2nd step)
  • Expected:
  1. A flow is matched, and the Response Body is the same as the local file

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

  • Input:
  1. Create a Map Local with a given JSON Flow
  2. Create an local JSON body -> Save it to the temporary folder for testing
  3. Ask the Map Entry to select a local file (created in the 2nd step)
  • Expected:
  1. A flow is matched
  2. The Response Header must have the Content-Type: application/json
  3. Response Status code is 200
  4. Response Body is exactly as a JSON Local File.

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

  • Input:
  1. Create a Map Local with a given JSON Flow
  2. Copy a small PNG image from the testing resource folder to the Temporary folder
  3. Ask the Map Entry to select a PNG File.
  • Expected:
  1. A flow is matched
  2. The Response Header must have the Content-Type: image/png
  3. Response Status code is 200
  4. Response Body data must be exactly the PNG data. We can encode the binary to base64 or HASH String, and compare the string.

6. Map Local, but not matching

  • Input:
  1. Create a Map Local with a given JSON Flow
  2. Ask the Map Entry to change the URL
  • Expected:
  1. A flow is not matched
  2. Response Status code is 200
    3 Response Body is exactly the HTTP Bin Response data (from the Real server)

7. Map Local from a given flow with GraphQL

  • Input:
  1. Create a Map Local with a given GraphQL Flow
  2. Ask the Map Entry to change some Header and Body.
  • Expected:
  1. A flow is matched
  2. The Map Local Entry must have the graphQLQueryName, and it matched the graphQL Query Name from the flow.
  3. Response Status code is 200
  4. Response Body and header are the same as we've updated

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

  • Input:
  1. Create a Map Local with a given GraphQL Flow
  2. Ask the Map Entry to change the GraphQL Query Name to a different name.
  • Expected:
  1. A flow is not matched! (Because the GraphQL is different even though the URL is the same)
  2. Response Status code is 200
  3. The response Body and header are the same with the real server (HTTP Bin)
Originally created by @NghiaTranUIT on GitHub (Mar 5, 2023). Original GitHub issue: https://github.com/ProxymanApp/proxyman-windows-linux/issues/209 Originally assigned to: @kics223w1 on GitHub. ## 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} ``` - Unit Test Strategy 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: ```swift 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 - Input: Create a Map Local with a default HTTP - Expected: A flow is matched, and the Response Body is the same as the default HTTP Raw Message ### 2. Map Local from a given flow - Input: Create a Map Local with a given JSON Flow -> Modify the status code, add some header, and change some body - Expected: A flow is matched, and the Response Body is the same as we've updated (Status code, Header, Body) ### 3. Map Local from a given flow - by selecting a local HTTP Message file - Input: 1. Create a Map Local with a given JSON Flow 2. Create an HTTP Message with some Status code, Header, and JSON body -> Save it to the temporary folder for testing 3. Ask the Map Entry to select a local file (created in the 2nd step) - Expected: 1. A flow is matched, and the Response Body is the same as the local file ### 4. Map Local from a given flow - by selecting a local JSON File - Input: 1. Create a Map Local with a given JSON Flow 2. Create an local JSON body -> Save it to the temporary folder for testing 3. Ask the Map Entry to select a local file (created in the 2nd step) - Expected: 1. A flow is matched 2. The Response Header must have the `Content-Type: application/json` 3. Response Status code is 200 4. Response Body is exactly as a JSON Local File. ### 5. Map Local from a given flow - by selecting a local Image PNG File (Binary) - Input: 1. Create a Map Local with a given JSON Flow 2. Copy a small PNG image from the testing resource folder to the Temporary folder 3. Ask the Map Entry to select a PNG File. - Expected: 1. A flow is matched 2. The Response Header must have the `Content-Type: image/png` 3. Response Status code is 200 4. Response Body data must be exactly the PNG data. We can encode the binary to base64 or HASH String, and compare the string. ### 6. Map Local, but not matching - Input: 1. Create a Map Local with a given JSON Flow 2. Ask the Map Entry to change the URL - Expected: 1. A flow is not matched 2. Response Status code is 200 3 Response Body is exactly the HTTP Bin Response data (from the Real server) ### 7. Map Local from a given flow with GraphQL - Input: 1. Create a Map Local with a given GraphQL Flow 2. Ask the Map Entry to change some Header and Body. - Expected: 1. A flow is matched 2. The Map Local Entry must have the graphQLQueryName, and it matched the graphQL Query Name from the flow. 3. Response Status code is 200 4. Response Body and header are the same as we've updated ### 78 Map Local from a given flow with GraphQL, but different graphQL Query Name - Input: 1. Create a Map Local with a given GraphQL Flow 2. Ask the Map Entry to change the GraphQL Query Name to a different name. - Expected: 1. A flow is not matched! (Because the GraphQL is different even though the URL is the same) 2. Response Status code is 200 3. The response Body and header are the same with the real server (HTTP Bin)
Author
Owner

@NghiaTranUIT commented on GitHub (Mar 5, 2023):

WildCardRegexTests Unit Tests

  • Ping @NghiaTranUIT to provide a Unit Tests file.
  • Port the Unit Test to typescript and make sure all unit tests are passed
<!-- gh-comment-id:1454974806 --> @NghiaTranUIT commented on GitHub (Mar 5, 2023): ### WildCardRegexTests Unit Tests - Ping @NghiaTranUIT to provide a Unit Tests file. - Port the Unit Test to typescript and make sure all unit tests are passed ✅
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/proxyman-windows-linux#210
No description provided.