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

//go:build response_snapshot

package sso

import (
	"bufio"
	"bytes"
	"context"
	"errors"
	"fmt"
	"github.com/aws/aws-sdk-go-v2/service/sso/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"
)

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_GetRoleCredentials(t *testing.T) {
	want := &GetRoleCredentialsOutput{
		RoleCredentials: &types.RoleCredentials{
			AccessKeyId:     ptr.String("__AccessKeyId__"),
			SecretAccessKey: ptr.String("__SecretAccessKey__"),
			SessionToken:    ptr.String("__SessionToken__"),
			Expiration:      1,
		},
	}
	status, header, body, err := serdeRespReadSnapshot("GetRoleCredentials.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.GetRoleCredentials(context.Background(), &GetRoleCredentialsInput{
		RoleName:    ptr.String("__RoleName__"),
		AccountId:   ptr.String("__AccountId__"),
		AccessToken: ptr.String("__AccessToken__"),
	})
	if err != nil {
		t.Fatal(err)
	}
	if err := smithytesting.CompareValues(want, got); err != nil {
		t.Errorf("response snapshot mismatch for %s: %v", "GetRoleCredentials.response", err)
	}
}

func TestCheckResponseSnapshot_ListAccountRoles(t *testing.T) {
	want := &ListAccountRolesOutput{
		NextToken: ptr.String("__NextToken__"),
		RoleList: []types.RoleInfo{
			{
				RoleName:  ptr.String("__RoleName__"),
				AccountId: ptr.String("__AccountId__"),
			},
			{
				RoleName:  ptr.String("__RoleName__"),
				AccountId: ptr.String("__AccountId__"),
			},
		},
	}
	status, header, body, err := serdeRespReadSnapshot("ListAccountRoles.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.ListAccountRoles(context.Background(), &ListAccountRolesInput{
		NextToken:   ptr.String("__NextToken__"),
		MaxResults:  ptr.Int32(1),
		AccessToken: ptr.String("__AccessToken__"),
		AccountId:   ptr.String("__AccountId__"),
	})
	if err != nil {
		t.Fatal(err)
	}
	if err := smithytesting.CompareValues(want, got); err != nil {
		t.Errorf("response snapshot mismatch for %s: %v", "ListAccountRoles.response", err)
	}
}

func TestCheckResponseSnapshot_ListAccounts(t *testing.T) {
	want := &ListAccountsOutput{
		NextToken: ptr.String("__NextToken__"),
		AccountList: []types.AccountInfo{
			{
				AccountId:    ptr.String("__AccountId__"),
				AccountName:  ptr.String("__AccountName__"),
				EmailAddress: ptr.String("__EmailAddress__"),
			},
			{
				AccountId:    ptr.String("__AccountId__"),
				AccountName:  ptr.String("__AccountName__"),
				EmailAddress: ptr.String("__EmailAddress__"),
			},
		},
	}
	status, header, body, err := serdeRespReadSnapshot("ListAccounts.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.ListAccounts(context.Background(), &ListAccountsInput{
		NextToken:   ptr.String("__NextToken__"),
		MaxResults:  ptr.Int32(1),
		AccessToken: ptr.String("__AccessToken__"),
	})
	if err != nil {
		t.Fatal(err)
	}
	if err := smithytesting.CompareValues(want, got); err != nil {
		t.Errorf("response snapshot mismatch for %s: %v", "ListAccounts.response", err)
	}
}

func TestCheckResponseSnapshot_Logout(t *testing.T) {
	want := &LogoutOutput{}
	status, header, body, err := serdeRespReadSnapshot("Logout.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.Logout(context.Background(), &LogoutInput{
		AccessToken: ptr.String("__AccessToken__"),
	})
	if err != nil {
		t.Fatal(err)
	}
	if err := smithytesting.CompareValues(want, got); err != nil {
		t.Errorf("response snapshot mismatch for %s: %v", "Logout.response", err)
	}
}

func TestCheckResponseSnapshot_Error_InvalidRequestException(t *testing.T) {
	want := &types.InvalidRequestException{
		Message: ptr.String("__Message__"),
	}
	status, header, body, err := serdeRespReadSnapshot("InvalidRequestException.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.GetRoleCredentials(context.Background(), &GetRoleCredentialsInput{
		RoleName:    ptr.String("__RoleName__"),
		AccountId:   ptr.String("__AccountId__"),
		AccessToken: ptr.String("__AccessToken__"),
	})
	if opErr == nil {
		t.Fatal("expected error, got nil")
	}
	var got *types.InvalidRequestException
	if !errors.As(opErr, &got) {
		t.Fatalf("expected types.InvalidRequestException, got %v", opErr)
	}
	if err := smithytesting.CompareValues(want, got); err != nil {
		t.Errorf("error response snapshot mismatch for %s: %v", "InvalidRequestException.error", err)
	}
}

func TestCheckResponseSnapshot_Error_ResourceNotFoundException(t *testing.T) {
	want := &types.ResourceNotFoundException{
		Message: ptr.String("__Message__"),
	}
	status, header, body, err := serdeRespReadSnapshot("ResourceNotFoundException.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.GetRoleCredentials(context.Background(), &GetRoleCredentialsInput{
		RoleName:    ptr.String("__RoleName__"),
		AccountId:   ptr.String("__AccountId__"),
		AccessToken: ptr.String("__AccessToken__"),
	})
	if opErr == nil {
		t.Fatal("expected error, got nil")
	}
	var got *types.ResourceNotFoundException
	if !errors.As(opErr, &got) {
		t.Fatalf("expected types.ResourceNotFoundException, got %v", opErr)
	}
	if err := smithytesting.CompareValues(want, got); err != nil {
		t.Errorf("error response snapshot mismatch for %s: %v", "ResourceNotFoundException.error", err)
	}
}

func TestCheckResponseSnapshot_Error_TooManyRequestsException(t *testing.T) {
	want := &types.TooManyRequestsException{
		Message: ptr.String("__Message__"),
	}
	status, header, body, err := serdeRespReadSnapshot("TooManyRequestsException.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.GetRoleCredentials(context.Background(), &GetRoleCredentialsInput{
		RoleName:    ptr.String("__RoleName__"),
		AccountId:   ptr.String("__AccountId__"),
		AccessToken: ptr.String("__AccessToken__"),
	})
	if opErr == nil {
		t.Fatal("expected error, got nil")
	}
	var got *types.TooManyRequestsException
	if !errors.As(opErr, &got) {
		t.Fatalf("expected types.TooManyRequestsException, got %v", opErr)
	}
	if err := smithytesting.CompareValues(want, got); err != nil {
		t.Errorf("error response snapshot mismatch for %s: %v", "TooManyRequestsException.error", err)
	}
}

func TestCheckResponseSnapshot_Error_UnauthorizedException(t *testing.T) {
	want := &types.UnauthorizedException{
		Message: ptr.String("__Message__"),
	}
	status, header, body, err := serdeRespReadSnapshot("UnauthorizedException.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.GetRoleCredentials(context.Background(), &GetRoleCredentialsInput{
		RoleName:    ptr.String("__RoleName__"),
		AccountId:   ptr.String("__AccountId__"),
		AccessToken: ptr.String("__AccessToken__"),
	})
	if opErr == nil {
		t.Fatal("expected error, got nil")
	}
	var got *types.UnauthorizedException
	if !errors.As(opErr, &got) {
		t.Fatalf("expected types.UnauthorizedException, got %v", opErr)
	}
	if err := smithytesting.CompareValues(want, got); err != nil {
		t.Errorf("error response snapshot mismatch for %s: %v", "UnauthorizedException.error", err)
	}
}
