From 7c354b26b3c7e4c9049273f47f05a07cd6b42734 Mon Sep 17 00:00:00 2001 From: Wanlin Du Date: Wed, 20 Aug 2025 20:23:23 +0000 Subject: [PATCH 1/2] fix the golang server for edge case: resposne is empty. --- internal/README.md | 116 ++++++++++++++++++ internal/replay/replay_http_server.go | 5 + .../sample/src/specs/sample.spec.ts | 4 +- 3 files changed, 123 insertions(+), 2 deletions(-) create mode 100644 internal/README.md diff --git a/internal/README.md b/internal/README.md new file mode 100644 index 0000000..f39b02f --- /dev/null +++ b/internal/README.md @@ -0,0 +1,116 @@ +# Development Guide: Testing Local test-server Changes + +## Overview +This guide outlines the standard procedure for compiling and testing local modifications to the Go test-server. The test-server is a Go application distributed via language-specific SDK wrappers for ease of adoption. + +The recommended workflow for end-to-end validation is to test through the SDK sample. This ensures that any changes to the core Go binary are compatible with the SDK integration layer. + +##Step-by-Step Testing Workflow using Typescript SDK + +The process involves compiling your local Go source code, preparing the TypeScript environment, and then linking your new binary to the sample project for testing. + +Step 1: Compile the Go Binary + +First, compile your local changes into an executable test-server binary. + +Navigate to the project's root directory (`test-server$`) and build the Go application. + +```sh +go build +``` + +After a successful build, a new executable named `test-server` will be present in the root directory. + +Step 2: Build the TypeScript SDK Wrapper + +The TypeScript SDK acts as a wrapper around the Go binary. It must be built before we can use it in the typescript samples. + +Navigate to the TypeScript SDK directory: test-server/sdks/typescript$. + +Install dependencies and build the SDK package. + +```sh +npm install +npm run build +``` + +Step 3: Prepare the Sample Test Project + +The sample project consumes the TypeScript SDK. We will set it up to run our tests. + +Navigate to the sample project directory: `test-server/sdks/typescript/samples$`. + +Install its dependencies. This command installs the test-server-sdk we just built in step 2. + +```sh +npm install +``` + +Step 4: Link Your Local test-server Binary +This is the most critical step. You must replace the pre-packaged binary in the sample project's dependencies with the custom binary you built in Step 1. + +After the previous steps, the directory structure looks like this. The goal is to replace the test-server binary highlighted below, the graph below is a tree view of your directory structure from `test-server/sdks/typescript/samples$`. + +```diff +├── dist +│   └── specs +│   ├── sample.spec.js +│   └── sample.spec.js.map +├── jasmine.json +├── node_modules +│   ├── ... (other packages hidden) +│  ├── test-server-sdk +│   │   ├── bin +│   │   │   ├── CHANGELOG.md +│   │   │   ├── LICENSE +│   │   │   ├── README.md ++│  │   │   └── test-server +│   │   ├── checksums.json +│   │   ├── LICENSE +│   │   ├── package.json +│   │   └── postinstall.js +│   └── ... (other packages hidden) +├── package.json +├── package-lock.json +├── src +│   └── specs +│   └── sample.spec.ts +├── test-data +│   ├── config +│   │   └── test-server-config.yml +│   └── recordings +└── tsconfig.json + +``` + +From the test-server/sdks/typescript/samples$ directory, run the following command to move your freshly compiled binary into place, overwriting the old one. + +```sh + +# Move the test-server binary from the project root to the samples' node_modules directory. +mv ../../../test-server ./node_modules/test-server-sdk/bin/test-server +``` + +Step 5: Run the Integration Tests + +You are now ready to run the sample project's test suite against your local test-server build. + +From the `test-server/sdks/typescript/samples$` directory: + +To run tests in playback mode: + +```sh +#To run tests in replay mode: +npm run test +#To run tests in record mode (and regenerate test recordings): +npm run test:record +``` + +Any output or errors from the test run will now reflect the behavior of your local test-server changes. + +You should also see the actual command run like this: + +``` +[test-server-sdk] Starting test-server in 'replay' mode. Command: /usr/local/google/home/test-server/sdks/typescript/sample/node_modules/test-server-sdk/bin/test-server replay --config /usr/local/google/home/wanlindu/test-server/sdks/typescript/sample/test-data/config/test-server-config.yml --recording-dir /usr/local/google/home/wanlindu/test-server/sdks/typescript/sample/test-data/recordings +``` +You can cross check if you have used the binary you just provided. \ No newline at end of file diff --git a/internal/replay/replay_http_server.go b/internal/replay/replay_http_server.go index 746d627..500e37c 100644 --- a/internal/replay/replay_http_server.go +++ b/internal/replay/replay_http_server.go @@ -178,6 +178,11 @@ func (r *ReplayHTTPServer) writeResponse(w http.ResponseWriter, resp *store.Reco w.Header().Add(key, value) } + // When the response body is empty we return directly with the headers. + if len(resp.BodySegments) == 0 { + return nil + } + w.WriteHeader(int(resp.StatusCode)) if !strings.Contains(req.URL, "alt=sse") { diff --git a/sdks/typescript/sample/src/specs/sample.spec.ts b/sdks/typescript/sample/src/specs/sample.spec.ts index b80c33b..5d82346 100644 --- a/sdks/typescript/sample/src/specs/sample.spec.ts +++ b/sdks/typescript/sample/src/specs/sample.spec.ts @@ -59,8 +59,8 @@ describe('Sample Test Suite (with test-server)', () => { }); res.on('end', () => { - expect(data.toLowerCase()).toContain('github'); - console.log('[SampleSpec] Received response, content check passed (basic).'); + expect(JSON.stringify(res.headers)).toContain('github'); + console.log('[SampleSpec] Received response. Because the body of the response can be empty, test headers instead.'); resolve(); }); }); From b0361ed9dcb4411d71dd3c0d0ee832d2b4f6d9e2 Mon Sep 17 00:00:00 2001 From: Wanlin Du Date: Wed, 20 Aug 2025 21:57:33 +0000 Subject: [PATCH 2/2] fix comment --- internal/README.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/internal/README.md b/internal/README.md index f39b02f..ce3a099 100644 --- a/internal/README.md +++ b/internal/README.md @@ -5,7 +5,7 @@ This guide outlines the standard procedure for compiling and testing local modif The recommended workflow for end-to-end validation is to test through the SDK sample. This ensures that any changes to the core Go binary are compatible with the SDK integration layer. -##Step-by-Step Testing Workflow using Typescript SDK +## Step-by-Step e2e Testing Workflow with TypeScript SDK The process involves compiling your local Go source code, preparing the TypeScript environment, and then linking your new binary to the sample project for testing. @@ -13,7 +13,7 @@ Step 1: Compile the Go Binary First, compile your local changes into an executable test-server binary. -Navigate to the project's root directory (`test-server$`) and build the Go application. +Navigate to the project's root directory (`test-server`) and build the Go application. ```sh go build @@ -23,9 +23,9 @@ After a successful build, a new executable named `test-server` will be present i Step 2: Build the TypeScript SDK Wrapper -The TypeScript SDK acts as a wrapper around the Go binary. It must be built before we can use it in the typescript samples. +The TypeScript SDK acts as a wrapper around the Go binary. It must be built before we can use it in the TypeScript samples. -Navigate to the TypeScript SDK directory: test-server/sdks/typescript$. +Navigate to the TypeScript SDK directory: `test-server/sdks/typescript`. Install dependencies and build the SDK package. @@ -38,7 +38,7 @@ Step 3: Prepare the Sample Test Project The sample project consumes the TypeScript SDK. We will set it up to run our tests. -Navigate to the sample project directory: `test-server/sdks/typescript/samples$`. +Navigate to the sample project directory: `test-server/sdks/typescript/samples`. Install its dependencies. This command installs the test-server-sdk we just built in step 2. @@ -49,7 +49,7 @@ npm install Step 4: Link Your Local test-server Binary This is the most critical step. You must replace the pre-packaged binary in the sample project's dependencies with the custom binary you built in Step 1. -After the previous steps, the directory structure looks like this. The goal is to replace the test-server binary highlighted below, the graph below is a tree view of your directory structure from `test-server/sdks/typescript/samples$`. +After the previous steps, the directory structure looks like this. The goal is to replace the test-server binary highlighted below, the graph below is a tree view of your directory structure from `test-server/sdks/typescript/samples`. ```diff ├── dist @@ -83,7 +83,7 @@ After the previous steps, the directory structure looks like this. The goal is t ``` -From the test-server/sdks/typescript/samples$ directory, run the following command to move your freshly compiled binary into place, overwriting the old one. +From the `test-server/sdks/typescript/samples` directory, run the following command to move your freshly compiled binary into place, overwriting the old one. ```sh @@ -95,7 +95,7 @@ Step 5: Run the Integration Tests You are now ready to run the sample project's test suite against your local test-server build. -From the `test-server/sdks/typescript/samples$` directory: +From the `test-server/sdks/typescript/samples` directory: To run tests in playback mode: