(t *testing.T)
| 319 | } |
| 320 | |
| 321 | func TestForwardResponseMessage(t *testing.T) { |
| 322 | msg := &pb.SimpleMessage{Id: "One"} |
| 323 | tests := []struct { |
| 324 | name string |
| 325 | marshaler runtime.Marshaler |
| 326 | contentType string |
| 327 | frw runtime.ForwardResponseRewriter |
| 328 | getWantedResponse func(msg any) ([]byte, error) |
| 329 | }{{ |
| 330 | name: "standard marshaler", |
| 331 | marshaler: &runtime.JSONPb{}, |
| 332 | contentType: "application/json", |
| 333 | }, { |
| 334 | name: "httpbody marshaler", |
| 335 | marshaler: &runtime.HTTPBodyMarshaler{&runtime.JSONPb{}}, |
| 336 | contentType: "application/json", |
| 337 | }, { |
| 338 | name: "custom marshaler", |
| 339 | marshaler: &CustomMarshaler{&runtime.JSONPb{}}, |
| 340 | contentType: "Custom-Content-Type", |
| 341 | }, { |
| 342 | name: "custom forward response rewriter", |
| 343 | marshaler: &runtime.JSONPb{}, |
| 344 | contentType: "application/json", |
| 345 | frw: func(ctx context.Context, response proto.Message) (any, error) { |
| 346 | return map[string]any{ |
| 347 | "ok": true, |
| 348 | "data": response, |
| 349 | }, nil |
| 350 | }, |
| 351 | getWantedResponse: func(msg any) ([]byte, error) { |
| 352 | return new(runtime.JSONPb).Marshal(map[string]any{"ok": true, "data": msg}) |
| 353 | }, |
| 354 | }} |
| 355 | |
| 356 | ctx := runtime.NewServerMetadataContext(context.Background(), runtime.ServerMetadata{}) |
| 357 | for _, tt := range tests { |
| 358 | t.Run(tt.name, func(t *testing.T) { |
| 359 | req := httptest.NewRequest("GET", "http://example.com/foo", nil) |
| 360 | resp := httptest.NewRecorder() |
| 361 | |
| 362 | opts := []runtime.ServeMuxOption{} |
| 363 | if tt.frw != nil { |
| 364 | opts = append(opts, runtime.WithForwardResponseRewriter(tt.frw)) |
| 365 | } |
| 366 | |
| 367 | runtime.ForwardResponseMessage(ctx, runtime.NewServeMux(opts...), tt.marshaler, resp, req, msg) |
| 368 | |
| 369 | w := resp.Result() |
| 370 | if w.StatusCode != http.StatusOK { |
| 371 | t.Errorf("StatusCode %d want %d", w.StatusCode, http.StatusOK) |
| 372 | } |
| 373 | if h := w.Header.Get("Content-Type"); h != tt.contentType { |
| 374 | t.Errorf("Content-Type %v want %v", h, tt.contentType) |
| 375 | } |
| 376 | body, err := io.ReadAll(w.Body) |
| 377 | if err != nil { |
| 378 | t.Errorf("Failed to read response body with %v", err) |
nothing calls this directly
no test coverage detected