// Code generated by smithy-go-codegen DO NOT EDIT.

//go:build response_snapshot

package smithyrpcv2cbor

import (
	"context"
	"errors"
	"github.com/aws/aws-sdk-go-v2/internal/protocoltest/smithyrpcv2cbor/schemas"
	"github.com/aws/aws-sdk-go-v2/internal/protocoltest/smithyrpcv2cbor/types"
	smithy "github.com/aws/smithy-go"
	smithycbor "github.com/aws/smithy-go/encoding/cbor"
	"github.com/aws/smithy-go/ptr"
	smithyhttp "github.com/aws/smithy-go/transport/http"
	"github.com/aws/smithy-go/transport/http/protocol/rpcv2"
	"io"
	"io/fs"
	"net/http"
	"os"
	"slices"
	"strconv"
	"strings"
	"testing"
	"time"
)

func serdeRespCreatePath(path string) (*os.File, error) {
	if err := os.MkdirAll(serdeRespSSPrefix, 0700); err != nil && !errors.Is(err, fs.ErrExist) {
		return nil, err
	}
	return os.Create(path)
}

func serdeRespWriteSnapshot(op string, status int, header http.Header, body []byte) error {
	f, err := serdeRespCreatePath(serdeRespSSPath(op))
	if err != nil {
		return err
	}
	defer f.Close()

	var sb strings.Builder
	sb.WriteString(strconv.Itoa(status))
	sb.WriteString("\n")
	keys := make([]string, 0, len(header))
	for k := range header {
		keys = append(keys, k)
	}
	slices.Sort(keys)
	for _, k := range keys {
		for _, v := range header[k] {
			sb.WriteString(k)
			sb.WriteString(": ")
			sb.WriteString(v)
			sb.WriteString("\n")
		}
	}
	sb.WriteString("\n")
	sb.Write(body)
	_, err = f.WriteString(sb.String())
	return err
}

// serdeRespXMLErrorEnvelope wraps serialized error members in the XML
// error envelope the restXml/query deserializers parse:
// <ErrorResponse><Error><Code>CODE</Code>MEMBERS</Error></ErrorResponse>.
// It strips the serialized body's outer root element and re-parents the
// members under <Error>.
func serdeRespXMLErrorEnvelope(body []byte, code string) []byte {
	inner := ""
	s := strings.TrimSpace(string(body))
	if strings.HasPrefix(s, "<?xml") {
		if i := strings.Index(s, "?>"); i >= 0 {
			s = strings.TrimSpace(s[i+2:])
		}
	}
	if start := strings.Index(s, ">"); start >= 0 {
		if end := strings.LastIndex(s, "<"); end > start {
			inner = s[start+1 : end]
		}
	}
	return []byte("<ErrorResponse><Error><Code>" + code + "</Code>" + inner + "</Error></ErrorResponse>")
}
func TestUpdateResponseSnapshot_EmptyInputOutput(t *testing.T) {
	want := &EmptyInputOutputOutput{}
	proto := rpcv2.NewCBOR(schemas.RpcV2Protocol)
	opSchema := smithy.NewOperationSchema(schemas.EmptyInputOutput, schemas.EmptyStructure, schemas.EmptyStructure)
	req := smithyhttp.NewStackRequest().(*smithyhttp.Request)
	if err := proto.SerializeRequest(context.Background(), opSchema, want, req); err != nil {
		t.Fatal(err)
	}
	built := req.Build(context.Background())
	var body []byte
	if built.Body != nil {
		b, err := io.ReadAll(built.Body)
		if err != nil {
			t.Fatal(err)
		}
		body = b
	}
	if err := serdeRespWriteSnapshot("EmptyInputOutput.response", 200, built.Header, body); err != nil {
		t.Fatal(err)
	}
}

func TestUpdateResponseSnapshot_Float16(t *testing.T) {
	want := &Float16Output{
		Value: ptr.Float64(1.0),
	}
	proto := rpcv2.NewCBOR(schemas.RpcV2Protocol)
	opSchema := smithy.NewOperationSchema(schemas.Float16, schemas.Float16Output, schemas.Float16Output)
	req := smithyhttp.NewStackRequest().(*smithyhttp.Request)
	if err := proto.SerializeRequest(context.Background(), opSchema, want, req); err != nil {
		t.Fatal(err)
	}
	built := req.Build(context.Background())
	var body []byte
	if built.Body != nil {
		b, err := io.ReadAll(built.Body)
		if err != nil {
			t.Fatal(err)
		}
		body = b
	}
	if err := serdeRespWriteSnapshot("Float16.response", 200, built.Header, body); err != nil {
		t.Fatal(err)
	}
}

func TestUpdateResponseSnapshot_FractionalSeconds(t *testing.T) {
	want := &FractionalSecondsOutput{
		Datetime: ptr.Time(time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)),
	}
	proto := rpcv2.NewCBOR(schemas.RpcV2Protocol)
	opSchema := smithy.NewOperationSchema(schemas.FractionalSeconds, schemas.FractionalSecondsOutput, schemas.FractionalSecondsOutput)
	req := smithyhttp.NewStackRequest().(*smithyhttp.Request)
	if err := proto.SerializeRequest(context.Background(), opSchema, want, req); err != nil {
		t.Fatal(err)
	}
	built := req.Build(context.Background())
	var body []byte
	if built.Body != nil {
		b, err := io.ReadAll(built.Body)
		if err != nil {
			t.Fatal(err)
		}
		body = b
	}
	if err := serdeRespWriteSnapshot("FractionalSeconds.response", 200, built.Header, body); err != nil {
		t.Fatal(err)
	}
}

func TestUpdateResponseSnapshot_GreetingWithErrors(t *testing.T) {
	want := &GreetingWithErrorsOutput{
		Greeting: ptr.String("__Greeting__"),
	}
	proto := rpcv2.NewCBOR(schemas.RpcV2Protocol)
	opSchema := smithy.NewOperationSchema(schemas.GreetingWithErrors, schemas.GreetingWithErrorsOutput, schemas.GreetingWithErrorsOutput)
	req := smithyhttp.NewStackRequest().(*smithyhttp.Request)
	if err := proto.SerializeRequest(context.Background(), opSchema, want, req); err != nil {
		t.Fatal(err)
	}
	built := req.Build(context.Background())
	var body []byte
	if built.Body != nil {
		b, err := io.ReadAll(built.Body)
		if err != nil {
			t.Fatal(err)
		}
		body = b
	}
	if err := serdeRespWriteSnapshot("GreetingWithErrors.response", 200, built.Header, body); err != nil {
		t.Fatal(err)
	}
}

func TestUpdateResponseSnapshot_NoInputOutput(t *testing.T) {
	want := &NoInputOutputOutput{}
	proto := rpcv2.NewCBOR(schemas.RpcV2Protocol)
	opSchema := smithy.NewOperationSchema(schemas.NoInputOutput, nil, nil)
	req := smithyhttp.NewStackRequest().(*smithyhttp.Request)
	if err := proto.SerializeRequest(context.Background(), opSchema, want, req); err != nil {
		t.Fatal(err)
	}
	built := req.Build(context.Background())
	var body []byte
	if built.Body != nil {
		b, err := io.ReadAll(built.Body)
		if err != nil {
			t.Fatal(err)
		}
		body = b
	}
	if err := serdeRespWriteSnapshot("NoInputOutput.response", 200, built.Header, body); err != nil {
		t.Fatal(err)
	}
}

func TestUpdateResponseSnapshot_OperationWithDefaults(t *testing.T) {
	want := &OperationWithDefaultsOutput{
		DefaultString:  ptr.String("__DefaultString__"),
		DefaultBoolean: ptr.Bool(true),
		DefaultList: []string{
			"__Member__",
			"__Member__",
		},
		DefaultTimestamp: ptr.Time(time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)),
		DefaultBlob:      []byte("blob"),
		DefaultByte:      ptr.Int8(1),
		DefaultShort:     ptr.Int16(1),
		DefaultInteger:   ptr.Int32(1),
		DefaultLong:      ptr.Int64(1),
		DefaultFloat:     ptr.Float32(1.0),
		DefaultDouble:    ptr.Float64(1.0),
		DefaultMap: map[string]string{
			"key0": "__Value__",
		},
		DefaultEnum:    types.TestEnum("FOO"),
		DefaultIntEnum: types.TestIntEnum(1),
		EmptyString:    ptr.String("__EmptyString__"),
		FalseBoolean:   true,
		EmptyBlob:      []byte("blob"),
		ZeroByte:       1,
		ZeroShort:      1,
		ZeroInteger:    1,
		ZeroLong:       1,
		ZeroFloat:      1.0,
		ZeroDouble:     1.0,
	}
	proto := rpcv2.NewCBOR(schemas.RpcV2Protocol)
	opSchema := smithy.NewOperationSchema(schemas.OperationWithDefaults, schemas.OperationWithDefaultsOutput, schemas.OperationWithDefaultsOutput)
	req := smithyhttp.NewStackRequest().(*smithyhttp.Request)
	if err := proto.SerializeRequest(context.Background(), opSchema, want, req); err != nil {
		t.Fatal(err)
	}
	built := req.Build(context.Background())
	var body []byte
	if built.Body != nil {
		b, err := io.ReadAll(built.Body)
		if err != nil {
			t.Fatal(err)
		}
		body = b
	}
	if err := serdeRespWriteSnapshot("OperationWithDefaults.response", 200, built.Header, body); err != nil {
		t.Fatal(err)
	}
}

func TestUpdateResponseSnapshot_OptionalInputOutput(t *testing.T) {
	want := &OptionalInputOutputOutput{
		Value: ptr.String("__Value__"),
	}
	proto := rpcv2.NewCBOR(schemas.RpcV2Protocol)
	opSchema := smithy.NewOperationSchema(schemas.OptionalInputOutput, schemas.SimpleStructure, schemas.SimpleStructure)
	req := smithyhttp.NewStackRequest().(*smithyhttp.Request)
	if err := proto.SerializeRequest(context.Background(), opSchema, want, req); err != nil {
		t.Fatal(err)
	}
	built := req.Build(context.Background())
	var body []byte
	if built.Body != nil {
		b, err := io.ReadAll(built.Body)
		if err != nil {
			t.Fatal(err)
		}
		body = b
	}
	if err := serdeRespWriteSnapshot("OptionalInputOutput.response", 200, built.Header, body); err != nil {
		t.Fatal(err)
	}
}

func TestUpdateResponseSnapshot_RecursiveShapes(t *testing.T) {
	want := &RecursiveShapesOutput{
		Nested: &types.RecursiveShapesInputOutputNested1{
			Foo: ptr.String("__Foo__"),
			Nested: &types.RecursiveShapesInputOutputNested2{
				Bar:             ptr.String("__Bar__"),
				RecursiveMember: nil,
			},
		},
	}
	proto := rpcv2.NewCBOR(schemas.RpcV2Protocol)
	opSchema := smithy.NewOperationSchema(schemas.RecursiveShapes, schemas.RecursiveShapesInputOutput, schemas.RecursiveShapesInputOutput)
	req := smithyhttp.NewStackRequest().(*smithyhttp.Request)
	if err := proto.SerializeRequest(context.Background(), opSchema, want, req); err != nil {
		t.Fatal(err)
	}
	built := req.Build(context.Background())
	var body []byte
	if built.Body != nil {
		b, err := io.ReadAll(built.Body)
		if err != nil {
			t.Fatal(err)
		}
		body = b
	}
	if err := serdeRespWriteSnapshot("RecursiveShapes.response", 200, built.Header, body); err != nil {
		t.Fatal(err)
	}
}

func TestUpdateResponseSnapshot_RpcV2CborDenseMaps(t *testing.T) {
	want := &RpcV2CborDenseMapsOutput{
		DenseStructMap: map[string]types.GreetingStruct{
			"key0": {
				Hi: ptr.String("__Hi__"),
			},
		},
		DenseNumberMap: map[string]int32{
			"key0": 1,
		},
		DenseBooleanMap: map[string]bool{
			"key0": true,
		},
		DenseStringMap: map[string]string{
			"key0": "__Value__",
		},
		DenseSetMap: map[string][]string{
			"key0": {
				"__Member__",
				"__Member__",
			},
		},
	}
	proto := rpcv2.NewCBOR(schemas.RpcV2Protocol)
	opSchema := smithy.NewOperationSchema(schemas.RpcV2CborDenseMaps, schemas.RpcV2CborDenseMapsInputOutput, schemas.RpcV2CborDenseMapsInputOutput)
	req := smithyhttp.NewStackRequest().(*smithyhttp.Request)
	if err := proto.SerializeRequest(context.Background(), opSchema, want, req); err != nil {
		t.Fatal(err)
	}
	built := req.Build(context.Background())
	var body []byte
	if built.Body != nil {
		b, err := io.ReadAll(built.Body)
		if err != nil {
			t.Fatal(err)
		}
		body = b
	}
	if err := serdeRespWriteSnapshot("RpcV2CborDenseMaps.response", 200, built.Header, body); err != nil {
		t.Fatal(err)
	}
}

func TestUpdateResponseSnapshot_RpcV2CborLists(t *testing.T) {
	want := &RpcV2CborListsOutput{
		StringList: []string{
			"__Member__",
			"__Member__",
		},
		StringSet: []string{
			"__Member__",
			"__Member__",
		},
		IntegerList: []int32{
			1,
			1,
		},
		BooleanList: []bool{
			true,
			true,
		},
		TimestampList: []time.Time{
			time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC),
			time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC),
		},
		EnumList: []types.FooEnum{
			types.FooEnum("Foo"),
			types.FooEnum("Foo"),
		},
		IntEnumList: []types.IntegerEnum{
			types.IntegerEnum(1),
			types.IntegerEnum(1),
		},
		NestedStringList: [][]string{
			{
				"__Member__",
				"__Member__",
			},
			{
				"__Member__",
				"__Member__",
			},
		},
		StructureList: []types.StructureListMember{
			{
				A: ptr.String("__A__"),
				B: ptr.String("__B__"),
			},
			{
				A: ptr.String("__A__"),
				B: ptr.String("__B__"),
			},
		},
		BlobList: [][]byte{
			[]byte("blob"),
			[]byte("blob"),
		},
	}
	proto := rpcv2.NewCBOR(schemas.RpcV2Protocol)
	opSchema := smithy.NewOperationSchema(schemas.RpcV2CborLists, schemas.RpcV2CborListInputOutput, schemas.RpcV2CborListInputOutput)
	req := smithyhttp.NewStackRequest().(*smithyhttp.Request)
	if err := proto.SerializeRequest(context.Background(), opSchema, want, req); err != nil {
		t.Fatal(err)
	}
	built := req.Build(context.Background())
	var body []byte
	if built.Body != nil {
		b, err := io.ReadAll(built.Body)
		if err != nil {
			t.Fatal(err)
		}
		body = b
	}
	if err := serdeRespWriteSnapshot("RpcV2CborLists.response", 200, built.Header, body); err != nil {
		t.Fatal(err)
	}
}

func TestUpdateResponseSnapshot_RpcV2CborSparseMaps(t *testing.T) {
	want := &RpcV2CborSparseMapsOutput{
		SparseStructMap: map[string]*types.GreetingStruct{
			"key0": {
				Hi: ptr.String("__Hi__"),
			},
		},
		SparseNumberMap: map[string]*int32{
			"key0": ptr.Int32(1),
		},
		SparseBooleanMap: map[string]*bool{
			"key0": ptr.Bool(true),
		},
		SparseStringMap: map[string]*string{
			"key0": ptr.String("__Value__"),
		},
		SparseSetMap: map[string][]string{
			"key0": {
				"__Member__",
				"__Member__",
			},
		},
	}
	proto := rpcv2.NewCBOR(schemas.RpcV2Protocol)
	opSchema := smithy.NewOperationSchema(schemas.RpcV2CborSparseMaps, schemas.RpcV2CborSparseMapsInputOutput, schemas.RpcV2CborSparseMapsInputOutput)
	req := smithyhttp.NewStackRequest().(*smithyhttp.Request)
	if err := proto.SerializeRequest(context.Background(), opSchema, want, req); err != nil {
		t.Fatal(err)
	}
	built := req.Build(context.Background())
	var body []byte
	if built.Body != nil {
		b, err := io.ReadAll(built.Body)
		if err != nil {
			t.Fatal(err)
		}
		body = b
	}
	if err := serdeRespWriteSnapshot("RpcV2CborSparseMaps.response", 200, built.Header, body); err != nil {
		t.Fatal(err)
	}
}

func TestUpdateResponseSnapshot_SimpleScalarProperties(t *testing.T) {
	want := &SimpleScalarPropertiesOutput{
		TrueBooleanValue:  ptr.Bool(true),
		FalseBooleanValue: ptr.Bool(true),
		ByteValue:         ptr.Int8(1),
		DoubleValue:       ptr.Float64(1.0),
		FloatValue:        ptr.Float32(1.0),
		IntegerValue:      ptr.Int32(1),
		LongValue:         ptr.Int64(1),
		ShortValue:        ptr.Int16(1),
		StringValue:       ptr.String("__StringValue__"),
		BlobValue:         []byte("blob"),
	}
	proto := rpcv2.NewCBOR(schemas.RpcV2Protocol)
	opSchema := smithy.NewOperationSchema(schemas.SimpleScalarProperties, schemas.SimpleScalarStructure, schemas.SimpleScalarStructure)
	req := smithyhttp.NewStackRequest().(*smithyhttp.Request)
	if err := proto.SerializeRequest(context.Background(), opSchema, want, req); err != nil {
		t.Fatal(err)
	}
	built := req.Build(context.Background())
	var body []byte
	if built.Body != nil {
		b, err := io.ReadAll(built.Body)
		if err != nil {
			t.Fatal(err)
		}
		body = b
	}
	if err := serdeRespWriteSnapshot("SimpleScalarProperties.response", 200, built.Header, body); err != nil {
		t.Fatal(err)
	}
}

func TestUpdateResponseSnapshot_SparseNullsOperation(t *testing.T) {
	want := &SparseNullsOperationOutput{
		SparseStringList: []*string{
			ptr.String("__Member__"),
			ptr.String("__Member__"),
		},
		SparseStringMap: map[string]*string{
			"key0": ptr.String("__Value__"),
		},
	}
	proto := rpcv2.NewCBOR(schemas.RpcV2Protocol)
	opSchema := smithy.NewOperationSchema(schemas.SparseNullsOperation, schemas.SparseNullsOperationInputOutput, schemas.SparseNullsOperationInputOutput)
	req := smithyhttp.NewStackRequest().(*smithyhttp.Request)
	if err := proto.SerializeRequest(context.Background(), opSchema, want, req); err != nil {
		t.Fatal(err)
	}
	built := req.Build(context.Background())
	var body []byte
	if built.Body != nil {
		b, err := io.ReadAll(built.Body)
		if err != nil {
			t.Fatal(err)
		}
		body = b
	}
	if err := serdeRespWriteSnapshot("SparseNullsOperation.response", 200, built.Header, body); err != nil {
		t.Fatal(err)
	}
}

func TestUpdateResponseSnapshot_Error_ValidationException(t *testing.T) {
	want := &types.ValidationException{
		Message: ptr.String("__Message__"),
		FieldList: []types.ValidationExceptionField{
			{
				Path:    ptr.String("__Path__"),
				Message: ptr.String("__Message__"),
			},
			{
				Path:    ptr.String("__Path__"),
				Message: ptr.String("__Message__"),
			},
		},
	}
	proto := rpcv2.NewCBOR(schemas.RpcV2Protocol)
	opSchema := smithy.NewOperationSchema(schemas.OperationWithDefaults, schemas.ValidationException, schemas.ValidationException)
	req := smithyhttp.NewStackRequest().(*smithyhttp.Request)
	if err := proto.SerializeRequest(context.Background(), opSchema, want, req); err != nil {
		t.Fatal(err)
	}
	built := req.Build(context.Background())
	var body []byte
	if built.Body != nil {
		b, err := io.ReadAll(built.Body)
		if err != nil {
			t.Fatal(err)
		}
		body = b
	}
	// Inject the CBOR error discriminator into the body map so the deserializer routes to the
	// modeled error type.
	var m smithycbor.Map
	if len(body) > 0 {
		v, err := smithycbor.Decode(body)
		if err != nil {
			t.Fatal(err)
		}
		mm, ok := v.(smithycbor.Map)
		if !ok {
			t.Fatalf("expected cbor map body, got %T", v)
		}
		m = mm
	} else {
		m = smithycbor.Map{}
	}
	m["__type"] = smithycbor.String(want.ErrorCode())
	body = smithycbor.Encode(m)
	if err := serdeRespWriteSnapshot("ValidationException.error", 400, built.Header, body); err != nil {
		t.Fatal(err)
	}
}

func TestUpdateResponseSnapshot_Error_ComplexError(t *testing.T) {
	want := &types.ComplexError{
		TopLevel: ptr.String("__TopLevel__"),
		Nested: &types.ComplexNestedErrorData{
			Foo: ptr.String("__Foo__"),
		},
	}
	proto := rpcv2.NewCBOR(schemas.RpcV2Protocol)
	opSchema := smithy.NewOperationSchema(schemas.GreetingWithErrors, schemas.ComplexError, schemas.ComplexError)
	req := smithyhttp.NewStackRequest().(*smithyhttp.Request)
	if err := proto.SerializeRequest(context.Background(), opSchema, want, req); err != nil {
		t.Fatal(err)
	}
	built := req.Build(context.Background())
	var body []byte
	if built.Body != nil {
		b, err := io.ReadAll(built.Body)
		if err != nil {
			t.Fatal(err)
		}
		body = b
	}
	// Inject the CBOR error discriminator into the body map so the deserializer routes to the
	// modeled error type.
	var m smithycbor.Map
	if len(body) > 0 {
		v, err := smithycbor.Decode(body)
		if err != nil {
			t.Fatal(err)
		}
		mm, ok := v.(smithycbor.Map)
		if !ok {
			t.Fatalf("expected cbor map body, got %T", v)
		}
		m = mm
	} else {
		m = smithycbor.Map{}
	}
	m["__type"] = smithycbor.String(want.ErrorCode())
	body = smithycbor.Encode(m)
	if err := serdeRespWriteSnapshot("ComplexError.error", 400, built.Header, body); err != nil {
		t.Fatal(err)
	}
}

func TestUpdateResponseSnapshot_Error_InvalidGreeting(t *testing.T) {
	want := &types.InvalidGreeting{
		Message: ptr.String("__Message__"),
	}
	proto := rpcv2.NewCBOR(schemas.RpcV2Protocol)
	opSchema := smithy.NewOperationSchema(schemas.GreetingWithErrors, schemas.InvalidGreeting, schemas.InvalidGreeting)
	req := smithyhttp.NewStackRequest().(*smithyhttp.Request)
	if err := proto.SerializeRequest(context.Background(), opSchema, want, req); err != nil {
		t.Fatal(err)
	}
	built := req.Build(context.Background())
	var body []byte
	if built.Body != nil {
		b, err := io.ReadAll(built.Body)
		if err != nil {
			t.Fatal(err)
		}
		body = b
	}
	// Inject the CBOR error discriminator into the body map so the deserializer routes to the
	// modeled error type.
	var m smithycbor.Map
	if len(body) > 0 {
		v, err := smithycbor.Decode(body)
		if err != nil {
			t.Fatal(err)
		}
		mm, ok := v.(smithycbor.Map)
		if !ok {
			t.Fatalf("expected cbor map body, got %T", v)
		}
		m = mm
	} else {
		m = smithycbor.Map{}
	}
	m["__type"] = smithycbor.String(want.ErrorCode())
	body = smithycbor.Encode(m)
	if err := serdeRespWriteSnapshot("InvalidGreeting.error", 400, built.Header, body); err != nil {
		t.Fatal(err)
	}
}
