|
| 1 | +package pluginhost |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "net/http" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/router-for-me/CLIProxyAPI/v7/sdk/pluginabi" |
| 10 | +) |
| 11 | + |
| 12 | +type staticEnvelopePluginClient struct { |
| 13 | + raw []byte |
| 14 | +} |
| 15 | + |
| 16 | +func (c staticEnvelopePluginClient) Call(context.Context, string, []byte) ([]byte, error) { |
| 17 | + return c.raw, nil |
| 18 | +} |
| 19 | + |
| 20 | +func (c staticEnvelopePluginClient) Shutdown() {} |
| 21 | + |
| 22 | +func TestDecodeEnvelopeResultPreservesPluginHTTPStatus(t *testing.T) { |
| 23 | + _, errDecode := decodeEnvelopeResult[rpcEmptyResponse](pluginabi.Envelope{ |
| 24 | + OK: false, |
| 25 | + Error: &pluginabi.Error{ |
| 26 | + Code: "plugin_error", |
| 27 | + Message: "license required", |
| 28 | + HTTPStatus: http.StatusForbidden, |
| 29 | + }, |
| 30 | + }) |
| 31 | + if errDecode == nil { |
| 32 | + t.Fatal("decodeEnvelopeResult returned nil error") |
| 33 | + } |
| 34 | + if got := errDecode.Error(); got != "license required" { |
| 35 | + t.Fatalf("error = %q, want license required", got) |
| 36 | + } |
| 37 | + statusProvider, ok := errDecode.(interface{ StatusCode() int }) |
| 38 | + if !ok { |
| 39 | + t.Fatalf("error %T does not expose StatusCode", errDecode) |
| 40 | + } |
| 41 | + if got := statusProvider.StatusCode(); got != http.StatusForbidden { |
| 42 | + t.Fatalf("status = %d, want %d", got, http.StatusForbidden) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +func TestCallPluginReturnsPluginErrorWithoutMethodWrapper(t *testing.T) { |
| 47 | + raw, errMarshal := json.Marshal(pluginabi.Envelope{ |
| 48 | + OK: false, |
| 49 | + Error: &pluginabi.Error{ |
| 50 | + Code: "plugin_error", |
| 51 | + Message: "license required", |
| 52 | + HTTPStatus: http.StatusForbidden, |
| 53 | + }, |
| 54 | + }) |
| 55 | + if errMarshal != nil { |
| 56 | + t.Fatalf("marshal envelope: %v", errMarshal) |
| 57 | + } |
| 58 | + _, errCall := callPlugin[rpcEmptyResponse](context.Background(), staticEnvelopePluginClient{raw: raw}, pluginabi.MethodExecutorExecuteStream, rpcEmptyResponse{}) |
| 59 | + if errCall == nil { |
| 60 | + t.Fatal("callPlugin returned nil error") |
| 61 | + } |
| 62 | + if got := errCall.Error(); got != "license required" { |
| 63 | + t.Fatalf("error = %q, want license required", got) |
| 64 | + } |
| 65 | + statusProvider, ok := errCall.(interface{ StatusCode() int }) |
| 66 | + if !ok { |
| 67 | + t.Fatalf("error %T does not expose StatusCode", errCall) |
| 68 | + } |
| 69 | + if got := statusProvider.StatusCode(); got != http.StatusForbidden { |
| 70 | + t.Fatalf("status = %d, want %d", got, http.StatusForbidden) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +func TestIsPluginErrorEnvelopeAcceptsNonzeroReturnEnvelope(t *testing.T) { |
| 75 | + raw := marshalRPCError("plugin_error", "upstream failed") |
| 76 | + if !isPluginErrorEnvelope(raw) { |
| 77 | + t.Fatalf("isPluginErrorEnvelope(%s) = false, want true", raw) |
| 78 | + } |
| 79 | + if isPluginErrorEnvelope([]byte(`not json`)) { |
| 80 | + t.Fatal("isPluginErrorEnvelope accepted invalid JSON") |
| 81 | + } |
| 82 | +} |
0 commit comments