namespace_test.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright 2014 Google Inc. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package appengine
  5. import (
  6. "strings"
  7. "testing"
  8. "github.com/golang/protobuf/proto"
  9. "golang.org/x/net/context"
  10. "google.golang.org/appengine/internal"
  11. "google.golang.org/appengine/internal/aetesting"
  12. basepb "google.golang.org/appengine/internal/base"
  13. )
  14. func TestNamespaceValidity(t *testing.T) {
  15. testCases := []struct {
  16. namespace string
  17. ok bool
  18. }{
  19. // data from Python's namespace_manager_test.py
  20. {"", true},
  21. {"__a.namespace.123__", true},
  22. {"-_A....NAMESPACE-_", true},
  23. {"-", true},
  24. {".", true},
  25. {".-", true},
  26. {"?", false},
  27. {"+", false},
  28. {"!", false},
  29. {" ", false},
  30. }
  31. for _, tc := range testCases {
  32. _, err := Namespace(context.Background(), tc.namespace)
  33. if err == nil && !tc.ok {
  34. t.Errorf("Namespace %q should be rejected, but wasn't", tc.namespace)
  35. } else if err != nil && tc.ok {
  36. t.Errorf("Namespace %q should be accepted, but wasn't", tc.namespace)
  37. }
  38. }
  39. }
  40. func TestNamespaceApplication(t *testing.T) {
  41. internal.NamespaceMods["srv"] = func(m proto.Message, namespace string) {
  42. sm := m.(*basepb.StringProto)
  43. if strings.Contains(sm.GetValue(), "-") {
  44. // be idempotent
  45. return
  46. }
  47. sm.Value = proto.String(sm.GetValue() + "-" + namespace)
  48. }
  49. ctx := aetesting.FakeSingleContext(t, "srv", "mth", func(in, out *basepb.StringProto) error {
  50. out.Value = proto.String(in.GetValue())
  51. return nil
  52. })
  53. call := func(ctx context.Context, in string) (out string, ok bool) {
  54. inm := &basepb.StringProto{Value: &in}
  55. outm := &basepb.StringProto{}
  56. if err := internal.Call(ctx, "srv", "mth", inm, outm); err != nil {
  57. t.Errorf("RPC(in=%q) failed: %v", in, err)
  58. return "", false
  59. }
  60. return outm.GetValue(), true
  61. }
  62. // Check without a namespace.
  63. got, ok := call(ctx, "foo")
  64. if !ok {
  65. t.FailNow()
  66. }
  67. if got != "foo" {
  68. t.Errorf("Un-namespaced RPC returned %q, want %q", got, "foo")
  69. }
  70. // Now check by applying a namespace.
  71. nsCtx, err := Namespace(ctx, "myns")
  72. if err != nil {
  73. t.Fatal(err)
  74. }
  75. got, ok = call(nsCtx, "bar")
  76. if !ok {
  77. t.FailNow()
  78. }
  79. if got != "bar-myns" {
  80. t.Errorf("Namespaced RPC returned %q, want %q", got, "bar-myns")
  81. }
  82. }