import json

from kat.harness import Query

from abstract_tests import AmbassadorTest, ServiceType, EGRPC

class AcceptanceGrpcBridgeTest(AmbassadorTest):

    target: ServiceType

    def init(self):
        self.target = EGRPC()

    def config(self):
        yield self, self.format("""
---
apiVersion: ambassador/v0
kind:  Module
name:  ambassador
config:
    enable_grpc_http11_bridge: True
""")

        yield self, self.format("""
---
apiVersion: ambassador/v0
kind:  Mapping
grpc: True
prefix: /echo.EchoService/
rewrite: /echo.EchoService/
name:  {self.target.path.k8s}
service: {self.target.path.k8s}
""")

    def queries(self):
        # [0]
        yield Query(self.url("echo.EchoService/Echo"), headers={ "content-type": "application/grpc", 
                                                                    "requested-status": "0" }, expected=200)

         # [1]
        yield Query(self.url("echo.EchoService/Echo"),  headers={ "content-type": "application/grpc", 
                                                                    "requested-status": "7" }, expected=200)

    def check(self):
        # [0]
        assert self.results[0].status == 200
        assert self.results[0].headers["Grpc-Status"] == ["0"]

        # [0]
        assert self.results[1].status == 200
        assert self.results[1].headers["Grpc-Status"] == ["7"]

from kat.harness import Query

from abstract_tests import AmbassadorTest, ServiceType, EGRPC

class AcceptanceGrpcBridgeTest(AmbassadorTest):

    target: ServiceType

    def init(self):
        self.target = EGRPC()

    def config(self):
        yield self, self.format("""
---
apiVersion: ambassador/v0
kind:  Module
name:  ambassador
config:
    enable_grpc_http11_bridge: True
""")

        yield self, self.format("""
---
apiVersion: ambassador/v0
kind:  Mapping
grpc: True
prefix: /echo.EchoService/
rewrite: /echo.EchoService/
name:  {self.target.path.k8s}
service: {self.target.path.k8s}
""")

    def queries(self):
        # [0]
        yield Query(self.url("echo.EchoService/Echo"),
                    headers={ "content-type": "application/grpc", "requested-status": "0" },
                    expected=200)

        # [1]
        yield Query(self.url("echo.EchoService/Echo"),
                    headers={ "content-type": "application/grpc", "requested-status": "7" },
                    expected=200)

    def check(self):
        # [0]
        assert self.results[0].headers["Grpc-Status"] == ["0"]

        # [1]
        assert self.results[1].headers["Grpc-Status"] == ["7"]

Clean up existing gRPC bridge test (make it like t_tcpmapping.py)
Clean up existing gRPC bridge test (make it like t_tcpmapping.py)

- Remove redundant HTTP status assertions
- Adjust formatting
- Remove unused import

- import json
- 
  from kat.harness import Query
  
  from abstract_tests import AmbassadorTest, ServiceType, EGRPC
  
  class AcceptanceGrpcBridgeTest(AmbassadorTest):
  
      target: ServiceType
  
      def init(self):
          self.target = EGRPC()
  
      def config(self):
          yield self, self.format("""
  ---
  apiVersion: ambassador/v0
  kind:  Module
  name:  ambassador
  config:
      enable_grpc_http11_bridge: True
  """)
  
          yield self, self.format("""
  ---
  apiVersion: ambassador/v0
  kind:  Mapping
  grpc: True
  prefix: /echo.EchoService/
  rewrite: /echo.EchoService/
  name:  {self.target.path.k8s}
  service: {self.target.path.k8s}
  """)
  
      def queries(self):
          # [0]
-         yield Query(self.url("echo.EchoService/Echo"), headers={ "content-type": "application/grpc", 
-                                                                     "requested-status": "0" }, expected=200)
+         yield Query(self.url("echo.EchoService/Echo"),
+                     headers={ "content-type": "application/grpc", "requested-status": "0" },
+                     expected=200)
  
-          # [1]
+         # [1]
-         yield Query(self.url("echo.EchoService/Echo"),  headers={ "content-type": "application/grpc", 
-                                                                     "requested-status": "7" }, expected=200)
+         yield Query(self.url("echo.EchoService/Echo"),
+                     headers={ "content-type": "application/grpc", "requested-status": "7" },
+                     expected=200)
  
      def check(self):
          # [0]
-         assert self.results[0].status == 200
          assert self.results[0].headers["Grpc-Status"] == ["0"]
  
-         # [0]
+         # [1]
-         assert self.results[1].status == 200
          assert self.results[1].headers["Grpc-Status"] == ["7"]
  
Clean up existing gRPC bridge test (make it like t_tcpmapping.py)
## Code Before:
import json

from kat.harness import Query

from abstract_tests import AmbassadorTest, ServiceType, EGRPC

class AcceptanceGrpcBridgeTest(AmbassadorTest):

    target: ServiceType

    def init(self):
        self.target = EGRPC()

    def config(self):
        yield self, self.format("""
---
apiVersion: ambassador/v0
kind:  Module
name:  ambassador
config:
    enable_grpc_http11_bridge: True
""")

        yield self, self.format("""
---
apiVersion: ambassador/v0
kind:  Mapping
grpc: True
prefix: /echo.EchoService/
rewrite: /echo.EchoService/
name:  {self.target.path.k8s}
service: {self.target.path.k8s}
""")

    def queries(self):
        # [0]
        yield Query(self.url("echo.EchoService/Echo"), headers={ "content-type": "application/grpc", 
                                                                    "requested-status": "0" }, expected=200)

         # [1]
        yield Query(self.url("echo.EchoService/Echo"),  headers={ "content-type": "application/grpc", 
                                                                    "requested-status": "7" }, expected=200)

    def check(self):
        # [0]
        assert self.results[0].status == 200
        assert self.results[0].headers["Grpc-Status"] == ["0"]

        # [0]
        assert self.results[1].status == 200
        assert self.results[1].headers["Grpc-Status"] == ["7"]

## Instruction:
Clean up existing gRPC bridge test (make it like t_tcpmapping.py)
## Code After:
from kat.harness import Query

from abstract_tests import AmbassadorTest, ServiceType, EGRPC

class AcceptanceGrpcBridgeTest(AmbassadorTest):

    target: ServiceType

    def init(self):
        self.target = EGRPC()

    def config(self):
        yield self, self.format("""
---
apiVersion: ambassador/v0
kind:  Module
name:  ambassador
config:
    enable_grpc_http11_bridge: True
""")

        yield self, self.format("""
---
apiVersion: ambassador/v0
kind:  Mapping
grpc: True
prefix: /echo.EchoService/
rewrite: /echo.EchoService/
name:  {self.target.path.k8s}
service: {self.target.path.k8s}
""")

    def queries(self):
        # [0]
        yield Query(self.url("echo.EchoService/Echo"),
                    headers={ "content-type": "application/grpc", "requested-status": "0" },
                    expected=200)

        # [1]
        yield Query(self.url("echo.EchoService/Echo"),
                    headers={ "content-type": "application/grpc", "requested-status": "7" },
                    expected=200)

    def check(self):
        # [0]
        assert self.results[0].headers["Grpc-Status"] == ["0"]

        # [1]
        assert self.results[1].headers["Grpc-Status"] == ["7"]
