github.com/anuvu/tyk@v2.9.0-beta9-dl-apic+incompatible/gateway/coprocess_bundle_test.go (about)

     1  // +build !python
     2  
     3  package gateway
     4  
     5  import (
     6  	"crypto/md5"
     7  	"fmt"
     8  	"io"
     9  	"os"
    10  	"path/filepath"
    11  	"testing"
    12  )
    13  
    14  var (
    15  	testBundlesPath = filepath.Join(testMiddlewarePath, "bundles")
    16  )
    17  
    18  var grpcBundleWithAuthCheck = map[string]string{
    19  	"manifest.json": `
    20  		{
    21  		    "file_list": [],
    22  		    "custom_middleware": {
    23  		        "driver": "grpc",
    24  		        "auth_check": {
    25  		            "name": "MyAuthHook"
    26  		        }
    27  		    }
    28  		}
    29  	`,
    30  }
    31  
    32  func TestBundleLoader(t *testing.T) {
    33  	bundleID := RegisterBundle("grpc_with_auth_check", grpcBundleWithAuthCheck)
    34  
    35  	t.Run("Nonexistent bundle", func(t *testing.T) {
    36  		specs := BuildAndLoadAPI(func(spec *APISpec) {
    37  			spec.CustomMiddlewareBundle = "nonexistent.zip"
    38  		})
    39  		err := loadBundle(specs[0])
    40  		if err == nil {
    41  			t.Fatal("Fetching a nonexistent bundle, expected an error")
    42  		}
    43  	})
    44  
    45  	t.Run("Existing bundle with auth check", func(t *testing.T) {
    46  		specs := BuildAndLoadAPI(func(spec *APISpec) {
    47  			spec.CustomMiddlewareBundle = bundleID
    48  		})
    49  		spec := specs[0]
    50  		err := loadBundle(spec)
    51  		if err != nil {
    52  			t.Fatalf("Bundle not found: %s\n", bundleID)
    53  		}
    54  
    55  		bundleNameHash := md5.New()
    56  		io.WriteString(bundleNameHash, spec.CustomMiddlewareBundle)
    57  		bundleDir := fmt.Sprintf("%s_%x", spec.APIID, bundleNameHash.Sum(nil))
    58  		savedBundlePath := filepath.Join(testBundlesPath, bundleDir)
    59  		if _, err = os.Stat(savedBundlePath); os.IsNotExist(err) {
    60  			t.Fatalf("Bundle wasn't saved to disk: %s", err.Error())
    61  		}
    62  
    63  		// Check bundle contents:
    64  		if spec.CustomMiddleware.AuthCheck.Name != "MyAuthHook" {
    65  			t.Fatalf("Auth check function doesn't match: got %s, expected %s\n", spec.CustomMiddleware.AuthCheck.Name, "MyAuthHook")
    66  		}
    67  		if string(spec.CustomMiddleware.Driver) != "grpc" {
    68  			t.Fatalf("Driver doesn't match: got %s, expected %s\n", spec.CustomMiddleware.Driver, "grpc")
    69  		}
    70  	})
    71  }