github.com/yandex/pandora@v0.5.32/components/providers/scenario/http/postprocessor/assert_response_test.go (about)

     1  package postprocessor
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"io"
     7  	"net/http"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestAssertResponse_Process(t *testing.T) {
    14  	type fields struct {
    15  		Headers    map[string]string
    16  		Body       []string
    17  		StatusCode int
    18  		Size       *AssertSize
    19  	}
    20  	type args struct {
    21  		resp *http.Response
    22  		body io.Reader
    23  	}
    24  	tests := []struct {
    25  		name    string
    26  		fields  fields
    27  		args    args
    28  		wantErr assert.ErrorAssertionFunc
    29  	}{
    30  		{
    31  			name: "Valid Response",
    32  			fields: fields{
    33  				Headers:    map[string]string{"Content-Type": "application/json"},
    34  				Body:       []string{"Hello, World!"},
    35  				StatusCode: http.StatusOK,
    36  				Size:       &AssertSize{Val: 13, Op: ">"},
    37  			},
    38  			args: args{
    39  				resp: &http.Response{StatusCode: http.StatusOK, Header: http.Header{"Content-Type": []string{"application/json"}}, Body: nil}, // Set body to nil for this example
    40  				body: bytes.NewReader([]byte(`{"message": "Hello, World!"}`)),
    41  			},
    42  			wantErr: assert.NoError,
    43  		},
    44  		{
    45  			name: "Invalid Header",
    46  			fields: fields{
    47  				Headers:    map[string]string{"Content-Type": "application/xml"},
    48  				Body:       []string{"Hello, World!"},
    49  				StatusCode: http.StatusOK,
    50  			},
    51  			args: args{
    52  				resp: &http.Response{StatusCode: http.StatusOK, Header: http.Header{"Content-Type": []string{"application/json"}}, Body: nil}, // Set body to nil for this example
    53  				body: bytes.NewReader([]byte(`{"message": "Hello, World!"}`)),
    54  			},
    55  			wantErr: assert.Error,
    56  		},
    57  		{
    58  			name: "Invalid Body",
    59  			fields: fields{
    60  				Headers:    map[string]string{"Content-Type": "application/json"},
    61  				Body:       []string{"Invalid Text"},
    62  				StatusCode: http.StatusOK,
    63  			},
    64  			args: args{
    65  				resp: &http.Response{StatusCode: http.StatusOK, Header: http.Header{"Content-Type": []string{"application/json"}}, Body: nil}, // Set body to nil for this example
    66  				body: bytes.NewReader([]byte(`{"message": "Hello, World!"}`)),
    67  			},
    68  			wantErr: assert.Error,
    69  		},
    70  		{
    71  			name: "Empty Body",
    72  			fields: fields{
    73  				Headers:    map[string]string{"Content-Type": "application/json"},
    74  				Body:       []string{"Hello, World!"},
    75  				StatusCode: http.StatusOK,
    76  			},
    77  			args: args{
    78  				resp: &http.Response{StatusCode: http.StatusOK, Header: http.Header{"Content-Type": []string{"application/json"}}, Body: nil}, // Set body to nil for this example
    79  				body: nil,                                                                                                                     // Set body to nil for this example
    80  			},
    81  			wantErr: assert.Error,
    82  		},
    83  		{
    84  			name: "Invalid StatusCode",
    85  			fields: fields{
    86  				Headers:    map[string]string{"Content-Type": "application/json"},
    87  				Body:       []string{"Hello, World!"},
    88  				StatusCode: http.StatusOK,
    89  			},
    90  			args: args{
    91  				resp: &http.Response{StatusCode: http.StatusNotFound, Header: http.Header{"Content-Type": []string{"application/json"}}, Body: nil}, // Set body to nil for this example
    92  				body: nil,                                                                                                                           // Set body to nil for this example
    93  			},
    94  			wantErr: assert.Error,
    95  		},
    96  		{
    97  			name: "Valid Size Assertion",
    98  			fields: fields{
    99  				Headers:    map[string]string{"Content-Type": "application/json"},
   100  				Body:       []string{"Hello, World!"},
   101  				StatusCode: http.StatusOK,
   102  				Size:       &AssertSize{Val: 28, Op: "eq"},
   103  			},
   104  			args: args{
   105  				resp: &http.Response{StatusCode: http.StatusOK, Header: http.Header{"Content-Type": []string{"application/json"}}, Body: nil}, // Set body to nil for this example
   106  				body: bytes.NewReader([]byte(`{"message": "Hello, World!"}`)),
   107  			},
   108  			wantErr: assert.NoError,
   109  		},
   110  		{
   111  			name: "Invalid Size Assertion",
   112  			fields: fields{
   113  				Headers:    map[string]string{"Content-Type": "application/json"},
   114  				Body:       []string{"Hello, World!"},
   115  				StatusCode: http.StatusOK,
   116  				Size:       &AssertSize{Val: 20, Op: "lt"},
   117  			},
   118  			args: args{
   119  				resp: &http.Response{StatusCode: http.StatusOK, Header: http.Header{"Content-Type": []string{"application/json"}}, Body: nil}, // Set body to nil for this example
   120  				body: bytes.NewReader([]byte(`{"message": "Hello, World!"}`)),
   121  			},
   122  			wantErr: assert.Error,
   123  		},
   124  		{
   125  			name: "Invalid Size Assertion",
   126  			fields: fields{
   127  				Headers:    map[string]string{"Content-Type": "application/json"},
   128  				Body:       []string{"Hello, World!"},
   129  				StatusCode: http.StatusOK,
   130  				Size:       &AssertSize{Val: 40, Op: "gt"},
   131  			},
   132  			args: args{
   133  				resp: &http.Response{StatusCode: http.StatusOK, Header: http.Header{"Content-Type": []string{"application/json"}}, Body: nil}, // Set body to nil for this example
   134  				body: bytes.NewReader([]byte(`{"message": "Hello, World!"}`)),
   135  			},
   136  			wantErr: assert.Error,
   137  		},
   138  		{
   139  			name: "Unknown Size Assertion Operator",
   140  			fields: fields{
   141  				Headers:    map[string]string{"Content-Type": "application/json"},
   142  				Body:       []string{"Hello, World!"},
   143  				StatusCode: http.StatusOK,
   144  				Size:       &AssertSize{Val: 13, Op: "unknown"},
   145  			},
   146  			args: args{
   147  				resp: &http.Response{StatusCode: http.StatusOK, Header: http.Header{"Content-Type": []string{"application/json"}}, Body: nil}, // Set body to nil for this example
   148  				body: bytes.NewReader([]byte(`{"message": "Hello, World!"}`)),
   149  			},
   150  			wantErr: assert.Error,
   151  		},
   152  	}
   153  	for _, tt := range tests {
   154  		t.Run(tt.name, func(t *testing.T) {
   155  			a := AssertResponse{
   156  				Headers:    tt.fields.Headers,
   157  				Body:       tt.fields.Body,
   158  				StatusCode: tt.fields.StatusCode,
   159  				Size:       tt.fields.Size,
   160  			}
   161  			process, err := a.Process(tt.args.resp, tt.args.body)
   162  			assert.Nil(t, process)
   163  			tt.wantErr(t, err, fmt.Sprintf("Process(%v, %v)", tt.args.resp, tt.args.body))
   164  		})
   165  	}
   166  }