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

//go:build response_snapshot

package apigatewaymanagementapi

import (
	"bufio"
	"bytes"
	"context"
	"errors"
	"fmt"
	"github.com/aws/aws-sdk-go-v2/service/apigatewaymanagementapi/types"
	smithyendpoints "github.com/aws/smithy-go/endpoints"
	"github.com/aws/smithy-go/middleware"
	"github.com/aws/smithy-go/ptr"
	smithytesting "github.com/aws/smithy-go/testing"
	smithyhttp "github.com/aws/smithy-go/transport/http"
	"io"
	"io/fs"
	"net/http"
	"net/url"
	"os"
	"strconv"
	"strings"
	"testing"
	"time"
)

const serdeRespSSPrefix = "response_snapshot"

func serdeRespSSPath(op string) string {
	return fmt.Sprintf("%s/%s.snap", serdeRespSSPrefix, op)
}

// serdeRespReadSnapshot parses a frozen wire response fixture: a status-code
// line, HTTP headers, a blank line, then the raw body.
func serdeRespReadSnapshot(op string) (int, http.Header, []byte, error) {
	raw, err := os.ReadFile(serdeRespSSPath(op))
	if err != nil {
		return 0, nil, nil, err
	}

	r := bufio.NewReader(bytes.NewReader(raw))
	statusLine, err := r.ReadString('\n')
	if err != nil {
		return 0, nil, nil, fmt.Errorf("read status line: %w", err)
	}
	status, err := strconv.Atoi(strings.TrimSpace(statusLine))
	if err != nil {
		return 0, nil, nil, fmt.Errorf("parse status: %w", err)
	}

	header := http.Header{}
	for {
		line, err := r.ReadString('\n')
		if err != nil && err != io.EOF {
			return 0, nil, nil, err
		}
		trimmed := strings.TrimRight(line, "\r\n")
		if trimmed == "" {
			break
		}
		k, v, ok := strings.Cut(trimmed, ": ")
		if !ok {
			return 0, nil, nil, fmt.Errorf("malformed header %q", trimmed)
		}
		header.Add(k, v)
		if err == io.EOF {
			break
		}
	}

	body, err := io.ReadAll(r)
	if err != nil {
		return 0, nil, nil, err
	}
	return status, header, body, nil
}

type serdeRespEndpointResolver struct{}

func (*serdeRespEndpointResolver) ResolveEndpoint(ctx context.Context, params EndpointParameters) (smithyendpoints.Endpoint, error) {
	return smithyendpoints.Endpoint{URI: url.URL{Scheme: "https", Host: "test.example.com"}}, nil
}

func serdeRespClient(status int, header http.Header, body []byte) *Client {
	return New(Options{
		Region: "us-east-1",
		HTTPClient: smithyhttp.ClientDoFunc(func(req *http.Request) (*http.Response, error) {
			resp := &http.Response{StatusCode: status, Header: header, Request: req}
			if len(body) > 0 {
				resp.ContentLength = int64(len(body))
				resp.Body = io.NopCloser(bytes.NewReader(body))
			} else {
				resp.Body = http.NoBody
			}
			return resp, nil
		}),
		EndpointResolverV2: &serdeRespEndpointResolver{},
		APIOptions: []func(*middleware.Stack) error{
			func(s *middleware.Stack) error {
				s.Finalize.Clear()
				s.Initialize.Remove("OperationInputValidation")
				return nil
			},
		},
	})
}
func TestCheckResponseSnapshot_DeleteConnection(t *testing.T) {
	want := &DeleteConnectionOutput{}
	status, header, body, err := serdeRespReadSnapshot("DeleteConnection.response")
	if errors.Is(err, fs.ErrNotExist) {
		t.Skip("no response snapshot fixture")
	}
	if err != nil {
		t.Fatal(err)
	}
	svc := serdeRespClient(status, header, body)
	got, err := svc.DeleteConnection(context.Background(), &DeleteConnectionInput{})
	if err != nil {
		t.Fatal(err)
	}
	if err := smithytesting.CompareValues(want, got); err != nil {
		t.Errorf("response snapshot mismatch for %s: %v", "DeleteConnection.response", err)
	}
}

func TestCheckResponseSnapshot_GetConnection(t *testing.T) {
	want := &GetConnectionOutput{
		ConnectedAt: ptr.Time(time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)),
		Identity: &types.Identity{
			SourceIp:  ptr.String("__SourceIp__"),
			UserAgent: ptr.String("__UserAgent__"),
		},
		LastActiveAt: ptr.Time(time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)),
	}
	status, header, body, err := serdeRespReadSnapshot("GetConnection.response")
	if errors.Is(err, fs.ErrNotExist) {
		t.Skip("no response snapshot fixture")
	}
	if err != nil {
		t.Fatal(err)
	}
	svc := serdeRespClient(status, header, body)
	got, err := svc.GetConnection(context.Background(), &GetConnectionInput{})
	if err != nil {
		t.Fatal(err)
	}
	if err := smithytesting.CompareValues(want, got); err != nil {
		t.Errorf("response snapshot mismatch for %s: %v", "GetConnection.response", err)
	}
}

func TestCheckResponseSnapshot_PostToConnection(t *testing.T) {
	want := &PostToConnectionOutput{}
	status, header, body, err := serdeRespReadSnapshot("PostToConnection.response")
	if errors.Is(err, fs.ErrNotExist) {
		t.Skip("no response snapshot fixture")
	}
	if err != nil {
		t.Fatal(err)
	}
	svc := serdeRespClient(status, header, body)
	got, err := svc.PostToConnection(context.Background(), &PostToConnectionInput{})
	if err != nil {
		t.Fatal(err)
	}
	if err := smithytesting.CompareValues(want, got); err != nil {
		t.Errorf("response snapshot mismatch for %s: %v", "PostToConnection.response", err)
	}
}

func TestCheckResponseSnapshot_Error_ForbiddenException(t *testing.T) {
	want := &types.ForbiddenException{}
	status, header, body, err := serdeRespReadSnapshot("ForbiddenException.error")
	if errors.Is(err, fs.ErrNotExist) {
		t.Skip("no response snapshot fixture")
	}
	if err != nil {
		t.Fatal(err)
	}
	svc := serdeRespClient(status, header, body)
	_, opErr := svc.DeleteConnection(context.Background(), &DeleteConnectionInput{})
	if opErr == nil {
		t.Fatal("expected error, got nil")
	}
	var got *types.ForbiddenException
	if !errors.As(opErr, &got) {
		t.Fatalf("expected types.ForbiddenException, got %v", opErr)
	}
	if err := smithytesting.CompareValues(want, got); err != nil {
		t.Errorf("error response snapshot mismatch for %s: %v", "ForbiddenException.error", err)
	}
}

func TestCheckResponseSnapshot_Error_GoneException(t *testing.T) {
	want := &types.GoneException{}
	status, header, body, err := serdeRespReadSnapshot("GoneException.error")
	if errors.Is(err, fs.ErrNotExist) {
		t.Skip("no response snapshot fixture")
	}
	if err != nil {
		t.Fatal(err)
	}
	svc := serdeRespClient(status, header, body)
	_, opErr := svc.DeleteConnection(context.Background(), &DeleteConnectionInput{})
	if opErr == nil {
		t.Fatal("expected error, got nil")
	}
	var got *types.GoneException
	if !errors.As(opErr, &got) {
		t.Fatalf("expected types.GoneException, got %v", opErr)
	}
	if err := smithytesting.CompareValues(want, got); err != nil {
		t.Errorf("error response snapshot mismatch for %s: %v", "GoneException.error", err)
	}
}

func TestCheckResponseSnapshot_Error_LimitExceededException(t *testing.T) {
	want := &types.LimitExceededException{}
	status, header, body, err := serdeRespReadSnapshot("LimitExceededException.error")
	if errors.Is(err, fs.ErrNotExist) {
		t.Skip("no response snapshot fixture")
	}
	if err != nil {
		t.Fatal(err)
	}
	svc := serdeRespClient(status, header, body)
	_, opErr := svc.DeleteConnection(context.Background(), &DeleteConnectionInput{})
	if opErr == nil {
		t.Fatal("expected error, got nil")
	}
	var got *types.LimitExceededException
	if !errors.As(opErr, &got) {
		t.Fatalf("expected types.LimitExceededException, got %v", opErr)
	}
	if err := smithytesting.CompareValues(want, got); err != nil {
		t.Errorf("error response snapshot mismatch for %s: %v", "LimitExceededException.error", err)
	}
}

func TestCheckResponseSnapshot_Error_PayloadTooLargeException(t *testing.T) {
	want := &types.PayloadTooLargeException{
		Message: ptr.String("__Message__"),
	}
	status, header, body, err := serdeRespReadSnapshot("PayloadTooLargeException.error")
	if errors.Is(err, fs.ErrNotExist) {
		t.Skip("no response snapshot fixture")
	}
	if err != nil {
		t.Fatal(err)
	}
	svc := serdeRespClient(status, header, body)
	_, opErr := svc.PostToConnection(context.Background(), &PostToConnectionInput{})
	if opErr == nil {
		t.Fatal("expected error, got nil")
	}
	var got *types.PayloadTooLargeException
	if !errors.As(opErr, &got) {
		t.Fatalf("expected types.PayloadTooLargeException, got %v", opErr)
	}
	if err := smithytesting.CompareValues(want, got); err != nil {
		t.Errorf("error response snapshot mismatch for %s: %v", "PayloadTooLargeException.error", err)
	}
}
