github.com/webonyx/up@v0.7.4-0.20180808230834-91b94e551323/internal/proxy/event_test.go (about)

     1  package proxy
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  )
     7  
     8  var getEvent = `{
     9    "resource": "/{proxy+}",
    10    "path": "/pets/tobi",
    11    "httpMethod": "GET",
    12    "headers": {
    13      "Accept": "*/*",
    14      "CloudFront-Forwarded-Proto": "https",
    15      "CloudFront-Is-Desktop-Viewer": "true",
    16      "CloudFront-Is-Mobile-Viewer": "false",
    17      "CloudFront-Is-SmartTV-Viewer": "false",
    18      "CloudFront-Is-Tablet-Viewer": "false",
    19      "CloudFront-Viewer-Country": "CA",
    20      "Host": "apex-ping.com",
    21      "User-Agent": "curl/7.48.0",
    22      "Via": "2.0 a44b4468444ef3ee67472bd5c5016098.cloudfront.net (CloudFront)",
    23      "X-Amz-Cf-Id": "VRxPGF8rOXD7xpRjAjseXfRrFD3wg-QPUHY6chzB9bR7pXlct1NTpg==",
    24      "X-Amzn-Trace-Id": "Root=1-59554c99-4375fc8705ccb554008b3aad",
    25      "X-Forwarded-For": "207.102.57.26, 54.182.214.69",
    26      "X-Forwarded-Port": "443",
    27      "X-Forwarded-Proto": "https"
    28    },
    29    "queryStringParameters": {
    30      "format": "json"
    31    },
    32    "pathParameters": {
    33      "proxy": "pets/tobi"
    34    },
    35    "stageVariables": {
    36      "env": "prod"
    37    },
    38    "requestContext": {
    39      "path": "/pets/tobi",
    40      "accountId": "111111111",
    41      "resourceId": "jcl9w3",
    42      "stage": "prod",
    43      "requestId": "344b184b-5cfc-11e7-8483-27dbb2d30a77",
    44      "identity": {
    45        "cognitoIdentityPoolId": null,
    46        "accountId": null,
    47        "cognitoIdentityId": null,
    48        "caller": null,
    49        "apiKey": "",
    50        "sourceIp": "207.102.57.26",
    51        "accessKey": null,
    52        "cognitoAuthenticationType": null,
    53        "cognitoAuthenticationProvider": null,
    54        "userArn": null,
    55        "userAgent": "curl/7.48.0",
    56        "user": null
    57      },
    58      "resourcePath": "/{proxy+}",
    59      "httpMethod": "GET",
    60      "apiId": "iwcgwgigca"
    61    },
    62    "body": null,
    63    "isBase64Encoded": false
    64  }`
    65  
    66  var getEventBasicAuth = `{
    67    "resource": "/{proxy+}",
    68    "path": "/pets/tobi",
    69    "httpMethod": "GET",
    70    "headers": {
    71      "Accept": "*/*",
    72      "CloudFront-Forwarded-Proto": "https",
    73      "CloudFront-Is-Desktop-Viewer": "true",
    74      "CloudFront-Is-Mobile-Viewer": "false",
    75      "CloudFront-Is-SmartTV-Viewer": "false",
    76      "CloudFront-Is-Tablet-Viewer": "false",
    77      "CloudFront-Viewer-Country": "CA",
    78      "Host": "apex-ping.com",
    79      "User-Agent": "curl/7.48.0",
    80      "Via": "2.0 a44b4468444ef3ee67472bd5c5016098.cloudfront.net (CloudFront)",
    81      "X-Amz-Cf-Id": "VRxPGF8rOXD7xpRjAjseXfRrFD3wg-QPUHY6chzB9bR7pXlct1NTpg==",
    82      "X-Amzn-Trace-Id": "Root=1-59554c99-4375fc8705ccb554008b3aad",
    83      "X-Forwarded-For": "207.102.57.26, 54.182.214.69",
    84      "X-Forwarded-Port": "443",
    85      "X-Forwarded-Proto": "https",
    86  		"Authorization": "Basic dG9iaTpmZXJyZXQ="
    87    },
    88    "queryStringParameters": {
    89      "format": "json"
    90    },
    91    "pathParameters": {
    92      "proxy": "pets/tobi"
    93    },
    94    "stageVariables": {
    95      "env": "prod"
    96    },
    97    "requestContext": {
    98      "path": "/pets/tobi",
    99      "accountId": "111111111",
   100      "resourceId": "jcl9w3",
   101      "stage": "prod",
   102      "requestId": "344b184b-5cfc-11e7-8483-27dbb2d30a77",
   103      "identity": {
   104        "cognitoIdentityPoolId": null,
   105        "accountId": null,
   106        "cognitoIdentityId": null,
   107        "caller": null,
   108        "apiKey": "",
   109        "sourceIp": "207.102.57.26",
   110        "accessKey": null,
   111        "cognitoAuthenticationType": null,
   112        "cognitoAuthenticationProvider": null,
   113        "userArn": null,
   114        "userAgent": "curl/7.48.0",
   115        "user": null
   116      },
   117      "resourcePath": "/{proxy+}",
   118      "httpMethod": "GET",
   119      "apiId": "iwcgwgigca"
   120    },
   121    "body": null,
   122    "isBase64Encoded": false
   123  }`
   124  
   125  var postEvent = `{
   126    "resource": "/{proxy+}",
   127    "path": "/pets/tobi",
   128    "httpMethod": "POST",
   129    "headers": {
   130      "Accept": "*/*",
   131      "CloudFront-Forwarded-Proto": "https",
   132      "CloudFront-Is-Desktop-Viewer": "true",
   133      "CloudFront-Is-Mobile-Viewer": "false",
   134      "CloudFront-Is-SmartTV-Viewer": "false",
   135      "CloudFront-Is-Tablet-Viewer": "false",
   136      "CloudFront-Viewer-Country": "CA",
   137      "content-type": "application/json",
   138      "Host": "apex-ping.com",
   139      "User-Agent": "curl/7.48.0",
   140      "Via": "2.0 b790a9f06b09414fec5d8b87e81d4b7f.cloudfront.net (CloudFront)",
   141      "X-Amz-Cf-Id": "_h1jFD3wjq6ZIyr8be6RS7Y7665jF9SjACmVodBMRefoQCs7KwTxMw==",
   142      "X-Amzn-Trace-Id": "Root=1-59554cc9-35de2f970f0fdf017f16927f",
   143      "X-Forwarded-For": "207.102.57.26, 54.182.214.86",
   144      "X-Forwarded-Port": "443",
   145      "X-Forwarded-Proto": "https"
   146    },
   147    "queryStringParameters": null,
   148    "pathParameters": {
   149      "proxy": "pets/tobi"
   150    },
   151    "requestContext": {
   152      "path": "/pets/tobi",
   153      "accountId": "111111111",
   154      "resourceId": "jcl9w3",
   155      "stage": "prod",
   156      "requestId": "50f6e0ce-5cfc-11e7-ada1-4f5cfe727f01",
   157      "identity": {
   158        "cognitoIdentityPoolId": null,
   159        "accountId": null,
   160        "cognitoIdentityId": null,
   161        "caller": null,
   162        "apiKey": "",
   163        "sourceIp": "207.102.57.26",
   164        "accessKey": null,
   165        "cognitoAuthenticationType": null,
   166        "cognitoAuthenticationProvider": null,
   167        "userArn": null,
   168        "userAgent": "curl/7.48.0",
   169        "user": null
   170      },
   171      "resourcePath": "/{proxy+}",
   172      "httpMethod": "POST",
   173      "apiId": "iwcgwgigca"
   174    },
   175    "body": "{ \"name\": \"Tobi\" }",
   176    "isBase64Encoded": false
   177  }`
   178  
   179  var postEventBinary = `{
   180    "resource": "/{proxy+}",
   181    "path": "/pets/tobi",
   182    "httpMethod": "POST",
   183    "headers": {
   184      "Accept": "*/*",
   185      "CloudFront-Forwarded-Proto": "https",
   186      "CloudFront-Is-Desktop-Viewer": "true",
   187      "CloudFront-Is-Mobile-Viewer": "false",
   188      "CloudFront-Is-SmartTV-Viewer": "false",
   189      "CloudFront-Is-Tablet-Viewer": "false",
   190      "CloudFront-Viewer-Country": "CA",
   191      "content-type": "text/plain",
   192      "Host": "apex-ping.com",
   193      "User-Agent": "curl/7.48.0",
   194      "Via": "2.0 b790a9f06b09414fec5d8b87e81d4b7f.cloudfront.net (CloudFront)",
   195      "X-Amz-Cf-Id": "_h1jFD3wjq6ZIyr8be6RS7Y7665jF9SjACmVodBMRefoQCs7KwTxMw==",
   196      "X-Amzn-Trace-Id": "Root=1-59554cc9-35de2f970f0fdf017f16927f",
   197      "X-Forwarded-For": "207.102.57.26, 54.182.214.86",
   198      "X-Forwarded-Port": "443",
   199      "X-Forwarded-Proto": "https"
   200    },
   201    "queryStringParameters": null,
   202    "pathParameters": {
   203      "proxy": "pets/tobi"
   204    },
   205    "requestContext": {
   206      "path": "/pets/tobi",
   207      "accountId": "111111111",
   208      "resourceId": "jcl9w3",
   209      "stage": "prod",
   210      "requestId": "50f6e0ce-5cfc-11e7-ada1-4f5cfe727f01",
   211      "identity": {
   212        "cognitoIdentityPoolId": null,
   213        "accountId": null,
   214        "cognitoIdentityId": null,
   215        "caller": null,
   216        "apiKey": "",
   217        "sourceIp": "207.102.57.26",
   218        "accessKey": null,
   219        "cognitoAuthenticationType": null,
   220        "cognitoAuthenticationProvider": null,
   221        "userArn": null,
   222        "userAgent": "curl/7.48.0",
   223        "user": null
   224      },
   225      "resourcePath": "/{proxy+}",
   226      "httpMethod": "POST",
   227      "apiId": "iwcgwgigca"
   228    },
   229    "body": "SGVsbG8gV29ybGQ=",
   230    "isBase64Encoded": true
   231  }`
   232  
   233  func output(v interface{}) {
   234  	b, _ := json.MarshalIndent(v, "", "  ")
   235  	fmt.Printf("%s\n", string(b))
   236  }
   237  
   238  func ExampleInput_get() {
   239  	var in Input
   240  	json.Unmarshal([]byte(getEvent), &in)
   241  	output(in)
   242  	// Output:
   243  	// {
   244  	//   "HTTPMethod": "GET",
   245  	//   "Headers": {
   246  	//     "Accept": "*/*",
   247  	//     "CloudFront-Forwarded-Proto": "https",
   248  	//     "CloudFront-Is-Desktop-Viewer": "true",
   249  	//     "CloudFront-Is-Mobile-Viewer": "false",
   250  	//     "CloudFront-Is-SmartTV-Viewer": "false",
   251  	//     "CloudFront-Is-Tablet-Viewer": "false",
   252  	//     "CloudFront-Viewer-Country": "CA",
   253  	//     "Host": "apex-ping.com",
   254  	//     "User-Agent": "curl/7.48.0",
   255  	//     "Via": "2.0 a44b4468444ef3ee67472bd5c5016098.cloudfront.net (CloudFront)",
   256  	//     "X-Amz-Cf-Id": "VRxPGF8rOXD7xpRjAjseXfRrFD3wg-QPUHY6chzB9bR7pXlct1NTpg==",
   257  	//     "X-Amzn-Trace-Id": "Root=1-59554c99-4375fc8705ccb554008b3aad",
   258  	//     "X-Forwarded-For": "207.102.57.26, 54.182.214.69",
   259  	//     "X-Forwarded-Port": "443",
   260  	//     "X-Forwarded-Proto": "https"
   261  	//   },
   262  	//   "Resource": "/{proxy+}",
   263  	//   "PathParameters": {
   264  	//     "proxy": "pets/tobi"
   265  	//   },
   266  	//   "Path": "/pets/tobi",
   267  	//   "QueryStringParameters": {
   268  	//     "format": "json"
   269  	//   },
   270  	//   "Body": "",
   271  	//   "IsBase64Encoded": false,
   272  	//   "StageVariables": {
   273  	//     "env": "prod"
   274  	//   },
   275  	//   "RequestContext": {
   276  	//     "apiId": "iwcgwgigca",
   277  	//     "resourceId": "jcl9w3",
   278  	//     "requestId": "344b184b-5cfc-11e7-8483-27dbb2d30a77",
   279  	//     "accountId": "111111111",
   280  	//     "stage": "prod",
   281  	//     "identity": {
   282  	//       "apiKey": "",
   283  	//       "accountId": "",
   284  	//       "userAgent": "curl/7.48.0",
   285  	//       "sourceIp": "207.102.57.26",
   286  	//       "accessKey": "",
   287  	//       "caller": "",
   288  	//       "user": "",
   289  	//       "userARN": "",
   290  	//       "cognitoIdentityId": "",
   291  	//       "cognitoIdentityPoolId": "",
   292  	//       "cognitoAuthenticationType": "",
   293  	//       "cognitoAuthenticationProvider": ""
   294  	//     },
   295  	//     "authorizer": null
   296  	//   }
   297  	// }
   298  }
   299  
   300  func ExampleInput_post() {
   301  	var in Input
   302  	json.Unmarshal([]byte(postEvent), &in)
   303  	output(in)
   304  	// Output:
   305  	// {
   306  	//   "HTTPMethod": "POST",
   307  	//   "Headers": {
   308  	//     "Accept": "*/*",
   309  	//     "CloudFront-Forwarded-Proto": "https",
   310  	//     "CloudFront-Is-Desktop-Viewer": "true",
   311  	//     "CloudFront-Is-Mobile-Viewer": "false",
   312  	//     "CloudFront-Is-SmartTV-Viewer": "false",
   313  	//     "CloudFront-Is-Tablet-Viewer": "false",
   314  	//     "CloudFront-Viewer-Country": "CA",
   315  	//     "Host": "apex-ping.com",
   316  	//     "User-Agent": "curl/7.48.0",
   317  	//     "Via": "2.0 b790a9f06b09414fec5d8b87e81d4b7f.cloudfront.net (CloudFront)",
   318  	//     "X-Amz-Cf-Id": "_h1jFD3wjq6ZIyr8be6RS7Y7665jF9SjACmVodBMRefoQCs7KwTxMw==",
   319  	//     "X-Amzn-Trace-Id": "Root=1-59554cc9-35de2f970f0fdf017f16927f",
   320  	//     "X-Forwarded-For": "207.102.57.26, 54.182.214.86",
   321  	//     "X-Forwarded-Port": "443",
   322  	//     "X-Forwarded-Proto": "https",
   323  	//     "content-type": "application/json"
   324  	//   },
   325  	//   "Resource": "/{proxy+}",
   326  	//   "PathParameters": {
   327  	//     "proxy": "pets/tobi"
   328  	//   },
   329  	//   "Path": "/pets/tobi",
   330  	//   "QueryStringParameters": null,
   331  	//   "Body": "{ \"name\": \"Tobi\" }",
   332  	//   "IsBase64Encoded": false,
   333  	//   "StageVariables": null,
   334  	//   "RequestContext": {
   335  	//     "apiId": "iwcgwgigca",
   336  	//     "resourceId": "jcl9w3",
   337  	//     "requestId": "50f6e0ce-5cfc-11e7-ada1-4f5cfe727f01",
   338  	//     "accountId": "111111111",
   339  	//     "stage": "prod",
   340  	//     "identity": {
   341  	//       "apiKey": "",
   342  	//       "accountId": "",
   343  	//       "userAgent": "curl/7.48.0",
   344  	//       "sourceIp": "207.102.57.26",
   345  	//       "accessKey": "",
   346  	//       "caller": "",
   347  	//       "user": "",
   348  	//       "userARN": "",
   349  	//       "cognitoIdentityId": "",
   350  	//       "cognitoIdentityPoolId": "",
   351  	//       "cognitoAuthenticationType": "",
   352  	//       "cognitoAuthenticationProvider": ""
   353  	//     },
   354  	//     "authorizer": null
   355  	//   }
   356  	// }
   357  }