github.com/Tyktechnologies/tyk@v2.9.5+incompatible/gateway/res_handler_transform_test.go (about)

     1  package gateway
     2  
     3  import (
     4  	"encoding/base64"
     5  	"testing"
     6  
     7  	"github.com/TykTechnologies/tyk/apidef"
     8  	"github.com/TykTechnologies/tyk/test"
     9  )
    10  
    11  func TestTransformResponseWithURLRewrite(t *testing.T) {
    12  	transformResponseConf := apidef.TemplateMeta{
    13  		Path:   "get",
    14  		Method: "GET",
    15  		TemplateData: apidef.TemplateData{
    16  			Mode:           "blob",
    17  			TemplateSource: base64.StdEncoding.EncodeToString([]byte(`{"http_method":"{{.Method}}"}`)),
    18  		},
    19  	}
    20  
    21  	urlRewriteConf := apidef.URLRewriteMeta{
    22  		Path:         "abc",
    23  		Method:       "GET",
    24  		MatchPattern: "abc",
    25  		RewriteTo:    "get",
    26  	}
    27  
    28  	responseProcessorConf := []apidef.ResponseProcessor{{Name: "response_body_transform"}}
    29  
    30  	t.Run("Transform without rewrite", func(t *testing.T) {
    31  		ts := StartTest()
    32  		defer ts.Close()
    33  
    34  		BuildAndLoadAPI(func(spec *APISpec) {
    35  			spec.Proxy.ListenPath = "/"
    36  			spec.ResponseProcessors = responseProcessorConf
    37  			UpdateAPIVersion(spec, "v1", func(v *apidef.VersionInfo) {
    38  				v.ExtendedPaths.TransformResponse = []apidef.TemplateMeta{transformResponseConf}
    39  			})
    40  		})
    41  
    42  		ts.Run(t, test.TestCase{
    43  			Path: "/get", Code: 200, BodyMatch: `{"http_method":"GET"}`,
    44  		})
    45  	})
    46  
    47  	t.Run("Transform path equals rewrite to ", func(t *testing.T) {
    48  		ts := StartTest()
    49  		defer ts.Close()
    50  
    51  		BuildAndLoadAPI(func(spec *APISpec) {
    52  			spec.Proxy.ListenPath = "/"
    53  			spec.ResponseProcessors = responseProcessorConf
    54  
    55  			UpdateAPIVersion(spec, "v1", func(v *apidef.VersionInfo) {
    56  				v.ExtendedPaths.TransformResponse = []apidef.TemplateMeta{transformResponseConf}
    57  				v.ExtendedPaths.URLRewrite = []apidef.URLRewriteMeta{urlRewriteConf}
    58  			})
    59  		})
    60  
    61  		ts.Run(t, test.TestCase{
    62  			Path: "/get", Code: 200, BodyMatch: `{"http_method":"GET"}`,
    63  		})
    64  	})
    65  
    66  	t.Run("Transform path equals rewrite path", func(t *testing.T) {
    67  		ts := StartTest()
    68  		defer ts.Close()
    69  
    70  		BuildAndLoadAPI(func(spec *APISpec) {
    71  			spec.Proxy.ListenPath = "/"
    72  			spec.ResponseProcessors = responseProcessorConf
    73  
    74  			transformResponseConf.Path = "abc"
    75  
    76  			UpdateAPIVersion(spec, "v1", func(v *apidef.VersionInfo) {
    77  				v.ExtendedPaths.TransformResponse = []apidef.TemplateMeta{transformResponseConf}
    78  				v.ExtendedPaths.URLRewrite = []apidef.URLRewriteMeta{urlRewriteConf}
    79  			})
    80  		})
    81  
    82  		ts.Run(t, test.TestCase{
    83  			Path: "/abc", Code: 200, BodyMatch: `{"http_method":"GET"}`,
    84  		})
    85  	})
    86  }
    87  
    88  func TestTransformResponse_ContextVars(t *testing.T) {
    89  	ts := StartTest()
    90  	defer ts.Close()
    91  
    92  	transformResponseConf := apidef.TemplateMeta{
    93  		Path:   "get",
    94  		Method: "GET",
    95  		TemplateData: apidef.TemplateData{
    96  			Mode:           "blob",
    97  			TemplateSource: base64.StdEncoding.EncodeToString([]byte(`{"foo":"{{._tyk_context.headers_Foo}}"}`)),
    98  		},
    99  	}
   100  
   101  	responseProcessorConf := []apidef.ResponseProcessor{{Name: "response_body_transform"}}
   102  
   103  	// When Context Vars are disabled
   104  	BuildAndLoadAPI(func(spec *APISpec) {
   105  		spec.Proxy.ListenPath = "/"
   106  		spec.ResponseProcessors = responseProcessorConf
   107  		UpdateAPIVersion(spec, "v1", func(v *apidef.VersionInfo) {
   108  			v.ExtendedPaths.TransformResponse = []apidef.TemplateMeta{transformResponseConf}
   109  		})
   110  	})
   111  
   112  	ts.Run(t, test.TestCase{
   113  		Headers: map[string]string{"Foo": "Bar"}, Path: "/get", Code: 200, BodyMatch: `{"foo":"<no value>"}`,
   114  	})
   115  
   116  	// When Context Vars are enabled
   117  	BuildAndLoadAPI(func(spec *APISpec) {
   118  		spec.Proxy.ListenPath = "/"
   119  		spec.EnableContextVars = true
   120  		spec.ResponseProcessors = responseProcessorConf
   121  		UpdateAPIVersion(spec, "v1", func(v *apidef.VersionInfo) {
   122  			v.ExtendedPaths.TransformResponse = []apidef.TemplateMeta{transformResponseConf}
   123  		})
   124  	})
   125  
   126  	ts.Run(t, test.TestCase{
   127  		Headers: map[string]string{"Foo": "Bar"}, Path: "/get", Code: 200, BodyMatch: `{"foo":"Bar"}`,
   128  	})
   129  }
   130  
   131  func TestTransformResponse_WithCache(t *testing.T) {
   132  	const path = "/get"
   133  
   134  	ts := StartTest()
   135  	defer ts.Close()
   136  
   137  	transformResponseConf := apidef.TemplateMeta{
   138  		Path:   path,
   139  		Method: "GET",
   140  		TemplateData: apidef.TemplateData{
   141  			Mode:           "blob",
   142  			TemplateSource: base64.StdEncoding.EncodeToString([]byte(`{"foo":"{{._tyk_context.headers_Foo}}"}`)),
   143  		},
   144  	}
   145  	responseProcessorConf := []apidef.ResponseProcessor{{Name: "response_body_transform"}}
   146  
   147  	createAPI := func(withCache bool) {
   148  		BuildAndLoadAPI(func(spec *APISpec) {
   149  			spec.Proxy.ListenPath = "/"
   150  			spec.CacheOptions.CacheTimeout = 60
   151  			spec.EnableContextVars = true
   152  			spec.CacheOptions.EnableCache = withCache
   153  			spec.ResponseProcessors = responseProcessorConf
   154  			UpdateAPIVersion(spec, "v1", func(v *apidef.VersionInfo) {
   155  				v.ExtendedPaths.TransformResponse = []apidef.TemplateMeta{transformResponseConf}
   156  				v.ExtendedPaths.Cached = []string{path}
   157  			})
   158  		})
   159  
   160  	}
   161  
   162  	// without cache
   163  	createAPI(false)
   164  
   165  	ts.Run(t, []test.TestCase{
   166  		{Path: path, Headers: map[string]string{"Foo": "Bar"}, Code: 200, BodyMatch: `{"foo":"Bar"}`},
   167  		{Path: path, Headers: map[string]string{"Foo": "Bar2"}, Code: 200, BodyMatch: `{"foo":"Bar2"}`},
   168  	}...)
   169  
   170  	// with cache
   171  	createAPI(true)
   172  
   173  	ts.Run(t, []test.TestCase{
   174  		{Path: path, Headers: map[string]string{"Foo": "Bar"}, Code: 200, BodyMatch: `{"foo":"Bar"}`},  // Returns response and caches it
   175  		{Path: path, Headers: map[string]string{"Foo": "Bar2"}, Code: 200, BodyMatch: `{"foo":"Bar"}`}, // Returns cached response directly
   176  	}...)
   177  
   178  }