github.com/webdestroya/awsmocker@v0.2.6/mocked_request_test.go (about)

     1  package awsmocker
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"net/url"
     7  	"regexp"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func TestMockedRequest_Inspect(t *testing.T) {
    14  	mr := &MockedRequest{
    15  		Strict:    true,
    16  		Hostname:  "awsmocker.local",
    17  		Service:   "sts",
    18  		Action:    "GetCallerIdentity",
    19  		Body:      "somebody",
    20  		Method:    http.MethodGet,
    21  		Path:      "/blah/blah/some/path",
    22  		PathRegex: regexp.MustCompile(`/version/([a-z0-9]+)/test`),
    23  		IsEc2IMDS: true,
    24  		Params: url.Values{
    25  			"TestParam": []string{"thing"},
    26  		},
    27  	}
    28  
    29  	result := mr.Inspect()
    30  
    31  	require.Contains(t, result, "STRICT")
    32  	require.Contains(t, result, "Service=sts")
    33  	require.Contains(t, result, "Action=GetCallerIdentity")
    34  	require.Contains(t, result, "imds=true")
    35  	require.Contains(t, result, "Hostname=awsmocker.local")
    36  	require.Contains(t, result, "Path=/blah/blah/some/path")
    37  	require.Contains(t, result, "Method=GET")
    38  	require.Contains(t, result, "Params=TestParam=thing")
    39  	require.Contains(t, result, "Body=somebody")
    40  }
    41  
    42  func TestMockedRequest_matchRequest(t *testing.T) {
    43  	type submatch struct {
    44  		matches bool
    45  		rr      *ReceivedRequest
    46  	}
    47  	tables := []struct {
    48  		name string
    49  		mr   *MockedRequest
    50  		reqs []submatch
    51  	}{
    52  		{
    53  			name: "SimpleServiceAction",
    54  			mr: &MockedRequest{
    55  				Service: "ecs",
    56  				Action:  "CreateService",
    57  			},
    58  			reqs: []submatch{
    59  				{
    60  					matches: true,
    61  					rr: &ReceivedRequest{
    62  						Service: "ecs",
    63  						Action:  "CreateService",
    64  					},
    65  				},
    66  				{
    67  					matches: false,
    68  					rr: &ReceivedRequest{
    69  						Service: "ecs",
    70  						Action:  "UpdateService",
    71  					},
    72  				},
    73  				{
    74  					matches: false,
    75  					rr: &ReceivedRequest{
    76  						Service: "sts",
    77  						Action:  "GetCallerIdentity",
    78  					},
    79  				},
    80  			},
    81  		},
    82  
    83  		{
    84  			name: "PathMatch",
    85  			mr: &MockedRequest{
    86  				Path: "/test",
    87  			},
    88  			reqs: []submatch{
    89  				{
    90  					matches: true,
    91  					rr: &ReceivedRequest{
    92  						Path:     "/test",
    93  						Hostname: "something.com",
    94  					},
    95  				},
    96  				{
    97  					matches: true,
    98  					rr: &ReceivedRequest{
    99  						Path:     "/test",
   100  						Hostname: "example.com",
   101  					},
   102  				},
   103  				{
   104  					matches: false,
   105  					rr: &ReceivedRequest{
   106  						Path:     "/texxxst",
   107  						Hostname: "something.com",
   108  					},
   109  				},
   110  			},
   111  		},
   112  
   113  		{
   114  			name: "PathRegex",
   115  			mr: &MockedRequest{
   116  				PathRegex: regexp.MustCompile(`/test/?.*`),
   117  			},
   118  			reqs: []submatch{
   119  				{
   120  					matches: true,
   121  					rr: &ReceivedRequest{
   122  						Path: "/test",
   123  					},
   124  				},
   125  				{
   126  					matches: false,
   127  					rr: &ReceivedRequest{
   128  						Path: "/xtest",
   129  					},
   130  				},
   131  				{
   132  					matches: true,
   133  					rr: &ReceivedRequest{
   134  						Path: "/test/thing",
   135  					},
   136  				},
   137  				{
   138  					matches: false,
   139  					rr: &ReceivedRequest{
   140  						Path: "/yar",
   141  					},
   142  				},
   143  			},
   144  		},
   145  
   146  		{
   147  			name: "BaseHostnameMatch",
   148  			mr: &MockedRequest{
   149  				Hostname: "test.com",
   150  			},
   151  			reqs: []submatch{
   152  				{
   153  					matches: true,
   154  					rr: &ReceivedRequest{
   155  						Hostname: "test.com",
   156  						Path:     "/test",
   157  					},
   158  				},
   159  				{
   160  					matches: false,
   161  					rr: &ReceivedRequest{
   162  						Hostname: "www.test.com",
   163  						Path:     "/xtest",
   164  					},
   165  				},
   166  				{
   167  					matches: false,
   168  					rr: &ReceivedRequest{
   169  						Path: "/xtest",
   170  					},
   171  				},
   172  			},
   173  		},
   174  
   175  		{
   176  			name: "BaseMethodMatch",
   177  			mr: &MockedRequest{
   178  				Method: http.MethodHead,
   179  			},
   180  			reqs: []submatch{
   181  				{
   182  					matches: true,
   183  					rr: &ReceivedRequest{
   184  						Hostname: "test.com",
   185  						Path:     "/test",
   186  						HttpRequest: &http.Request{
   187  							Method: http.MethodHead,
   188  						},
   189  					},
   190  				},
   191  				{
   192  					matches: false,
   193  					rr: &ReceivedRequest{
   194  						Hostname: "test.com",
   195  						Path:     "/test",
   196  						HttpRequest: &http.Request{
   197  							Method: http.MethodGet,
   198  						},
   199  					},
   200  				},
   201  				{
   202  					matches: false,
   203  					rr: &ReceivedRequest{
   204  						Hostname: "test.com",
   205  						Path:     "/test",
   206  						HttpRequest: &http.Request{
   207  							Method: http.MethodPost,
   208  						},
   209  					},
   210  				},
   211  			},
   212  		},
   213  
   214  		{
   215  			name: "CustomMatcher",
   216  			mr: &MockedRequest{
   217  				Matcher: func(rr *ReceivedRequest) bool {
   218  					return rr.Path == "/test" || rr.Hostname == "fake.com" || rr.Service == "ec2"
   219  				},
   220  			},
   221  			reqs: []submatch{
   222  				{
   223  					matches: true,
   224  					rr: &ReceivedRequest{
   225  						Path: "/test",
   226  					},
   227  				},
   228  				{
   229  					matches: true,
   230  					rr: &ReceivedRequest{
   231  						Path:     "/something",
   232  						Hostname: "fake.com",
   233  					},
   234  				},
   235  				{
   236  					matches: true,
   237  					rr: &ReceivedRequest{
   238  						Service: "ec2",
   239  						Action:  "TerminateInstance",
   240  					},
   241  				},
   242  				{
   243  					matches: false,
   244  					rr: &ReceivedRequest{
   245  						Path: "/blah",
   246  					},
   247  				},
   248  			},
   249  		},
   250  
   251  		{
   252  			name: "JMESpath",
   253  			mr: &MockedRequest{
   254  				JMESPathMatches: map[string]any{
   255  					"thing.test[0]": "foo",
   256  				},
   257  			},
   258  			reqs: []submatch{
   259  				{
   260  					matches: true,
   261  					rr: &ReceivedRequest{
   262  						Hostname: imdsHost4,
   263  						Path:     "/test",
   264  						JsonPayload: map[string]any{
   265  							"thing": map[string]any{
   266  								"test": []string{"foo", "blah"},
   267  							},
   268  						},
   269  					},
   270  				},
   271  				{
   272  					matches: false,
   273  					rr: &ReceivedRequest{
   274  						Hostname: imdsHost6,
   275  						Path:     "/test",
   276  					},
   277  				},
   278  				{
   279  					matches: false,
   280  					rr: &ReceivedRequest{
   281  						Hostname:    imdsHost6,
   282  						Path:        "/test",
   283  						JsonPayload: make(map[string]any),
   284  					},
   285  				},
   286  				{
   287  					matches: false,
   288  					rr: &ReceivedRequest{
   289  						Hostname: imdsHost4,
   290  						Path:     "/test",
   291  						JsonPayload: map[string]any{
   292  							"thing": map[string]any{
   293  								"test": []string{"yar", "blah"},
   294  							},
   295  						},
   296  					},
   297  				},
   298  			},
   299  		},
   300  
   301  		{
   302  			name: "EC2Metadata",
   303  			mr: &MockedRequest{
   304  				IsEc2IMDS: true,
   305  			},
   306  			reqs: []submatch{
   307  				{
   308  					matches: true,
   309  					rr: &ReceivedRequest{
   310  						Hostname: imdsHost4,
   311  						Path:     "/test",
   312  					},
   313  				},
   314  				{
   315  					matches: true,
   316  					rr: &ReceivedRequest{
   317  						Hostname: imdsHost6,
   318  						Path:     "/test",
   319  					},
   320  				},
   321  				{
   322  					matches: false,
   323  					rr: &ReceivedRequest{
   324  						Path:     "/something",
   325  						Hostname: "127.0.0.1",
   326  					},
   327  				},
   328  			},
   329  		},
   330  
   331  		{
   332  			name: "ParamsMatcher",
   333  			mr: &MockedRequest{
   334  				Service: "ecs",
   335  				Action:  "CreateService",
   336  				Params: url.Values{
   337  					"Names.member.1": []string{"test"},
   338  				},
   339  			},
   340  			reqs: []submatch{
   341  				{
   342  					matches: true,
   343  					rr: &ReceivedRequest{
   344  						Service: "ecs",
   345  						Action:  "CreateService",
   346  						HttpRequest: &http.Request{
   347  							Form: url.Values{
   348  								"Names.member.1": []string{"test"},
   349  							},
   350  						},
   351  					},
   352  				},
   353  				{
   354  					matches: true,
   355  					rr: &ReceivedRequest{
   356  						Service: "ecs",
   357  						Action:  "CreateService",
   358  						HttpRequest: &http.Request{
   359  							Form: url.Values{
   360  								"Names.member.1":     []string{"test"},
   361  								"Something.member.1": []string{"xxx"},
   362  							},
   363  						},
   364  					},
   365  				},
   366  				{
   367  					matches: false,
   368  					rr: &ReceivedRequest{
   369  						Service: "ecs",
   370  						Action:  "CreateService",
   371  						HttpRequest: &http.Request{
   372  							Form: url.Values{
   373  								"Names.member.1": []string{"test", "thing"},
   374  							},
   375  						},
   376  					},
   377  				},
   378  				{
   379  					matches: false,
   380  					rr: &ReceivedRequest{
   381  						Service: "ecs",
   382  						Action:  "CreateService",
   383  						HttpRequest: &http.Request{
   384  							Form: url.Values{
   385  								"Names.member.1": []string{"somethingelse"},
   386  							},
   387  						},
   388  					},
   389  				},
   390  				{
   391  					matches: false,
   392  					rr: &ReceivedRequest{
   393  						Service: "ecs",
   394  						Action:  "CreateService",
   395  						HttpRequest: &http.Request{
   396  							Form: url.Values{
   397  								"Something.Else.1": []string{"somethingelse"},
   398  							},
   399  						},
   400  					},
   401  				},
   402  				{
   403  					matches: false,
   404  					rr: &ReceivedRequest{
   405  						Service:     "ecs",
   406  						Action:      "CreateService",
   407  						HttpRequest: &http.Request{},
   408  					},
   409  				},
   410  			},
   411  		},
   412  
   413  		{
   414  			name: "StrictParamsMatcher",
   415  			mr: &MockedRequest{
   416  				Service: "ecs",
   417  				Action:  "CreateService",
   418  				Strict:  true,
   419  				Params: url.Values{
   420  					"Names.member.1": []string{"test"},
   421  				},
   422  			},
   423  			reqs: []submatch{
   424  				{
   425  					matches: true,
   426  					rr: &ReceivedRequest{
   427  						Service: "ecs",
   428  						Action:  "CreateService",
   429  						HttpRequest: &http.Request{
   430  							Form: url.Values{
   431  								"Names.member.1": []string{"test"},
   432  							},
   433  						},
   434  					},
   435  				},
   436  				{
   437  					matches: false,
   438  					rr: &ReceivedRequest{
   439  						Service: "ecs",
   440  						Action:  "CreateService",
   441  						HttpRequest: &http.Request{
   442  							Form: url.Values{
   443  								"Names.member.1":     []string{"test"},
   444  								"Something.member.1": []string{"xxx"},
   445  							},
   446  						},
   447  					},
   448  				},
   449  			},
   450  		},
   451  	}
   452  
   453  	for _, table := range tables {
   454  
   455  		t.Run(table.name, func(t *testing.T) {
   456  
   457  			for x, req := range table.reqs {
   458  				t.Run(fmt.Sprintf("test_%d", x+1), func(t *testing.T) {
   459  					require.Equal(t, req.matches, table.mr.matchRequest(req.rr))
   460  				})
   461  			}
   462  		})
   463  	}
   464  }