Repository: getmoto/moto
Pull request #7038 (2023-11-17 13:40:15)

Lambda: no log response bug
Resolves https://github.com/getmoto/moto/issues/7033

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/getmoto/moto/pull/7038?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=getmoto) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Comparison is base [(`b548635`)](https://app.codecov.io/gh/getmoto/moto/commit/b54863523be3c65f682b18ef80c1c27348dfeca3?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=getmoto) 95.85% compared to head [(`7e97eb6`)](https://app.codecov.io/gh/getmoto/moto/pull/7038?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=getmoto) 95.85%.


<details><summary>Additional details and impacted files</summary>


```diff
@@           Coverage Diff           @@
##           master    #7038   +/-   ##
=======================================
  Coverage   95.85%   95.85%           
=======================================
  Files         834      834           
  Lines       81711    81713    +2     
=======================================
+ Hits        78324    78326    +2     
  Misses       3387     3387           
```

| [Flag](https://app.codecov.io/gh/getmoto/moto/pull/7038/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=getmoto) | Coverage Δ | |
|---|---|---|
| [servertests](https://app.codecov.io/gh/getmoto/moto/pull/7038/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=getmoto) | `36.64% <0.00%> (-0.01%)` | :arrow_down: |
| [unittests](https://app.codecov.io/gh/getmoto/moto/pull/7038/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=getmoto) | `95.79% <100.00%> (+<0.01%)` | :arrow_up: |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=getmoto#carryforward-flags-in-the-pull-request-comment) to find out more.


</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/getmoto/moto/pull/7038?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=getmoto).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=getmoto).


Patch:
diff --git a/moto/awslambda/responses.py b/moto/awslambda/responses.py
index b00dc7138..c06eb7b69 100644
--- a/moto/awslambda/responses.py
+++ b/moto/awslambda/responses.py
@@ -269,7 +269,10 @@ class LambdaResponse(BaseResponse):
             elif request.headers.get("X-Amz-Invocation-Type") == "DryRun":
                 status_code = 204
             else:
-                if request.headers.get("X-Amz-Log-Type") != "Tail":
+                if (
+                    request.headers.get("X-Amz-Log-Type") != "Tail"
+                    and "x-amz-log-result" in response_headers
+                ):
                     del response_headers["x-amz-log-result"]
                 status_code = 200
             return status_code, response_headers, payload
diff --git a/moto/awslambda_simple/models.py b/moto/awslambda_simple/models.py
index b1446b13e..68b7e32aa 100644
--- a/moto/awslambda_simple/models.py
+++ b/moto/awslambda_simple/models.py
@@ -51,6 +51,8 @@ class LambdaSimpleBackend(BaseBackend):
         response_headers: Any,
     ) -> Optional[Union[str, bytes]]:
 
+        if body:
+            return str.encode(body)
         return b"Simple Lambda happy path OK"
 
 


Test patch:
diff --git a/tests/test_awslambda_simple/test_lambda_simple.py b/tests/test_awslambda_simple/test_lambda_simple.py
index 3d0dccfd2..e316b7890 100644
--- a/tests/test_awslambda_simple/test_lambda_simple.py
+++ b/tests/test_awslambda_simple/test_lambda_simple.py
@@ -1,3 +1,4 @@
+import json
 from unittest import SkipTest
 import boto3
 
@@ -15,6 +16,36 @@ if settings.TEST_SERVER_MODE:
 @mock_iam
 @mock_lambda_simple
 def test_run_function():
+    # Setup
+    client = setup_lambda()
+
+    # Execute
+    result = client.invoke(
+        FunctionName=FUNCTION_NAME,
+        LogType="Tail",
+    )
+
+    # Verify
+    assert result["StatusCode"] == 200
+    assert result["Payload"].read().decode("utf-8") == "Simple Lambda happy path OK"
+
+
+@mock_iam
+@mock_lambda_simple
+def test_run_function_no_log():
+    # Setup
+    client = setup_lambda()
+    payload = {"results": "results"}
+
+    # Execute
+    result = client.invoke(FunctionName=FUNCTION_NAME, Payload=json.dumps(payload))
+
+    # Verify
+    assert result["StatusCode"] == 200
+    assert json.loads(result["Payload"].read().decode("utf-8")) == payload
+
+
+def setup_lambda():
     client = boto3.client("lambda", LAMBDA_REGION)
     zip_content = get_test_zip_file1()
     function_name = FUNCTION_NAME
@@ -30,10 +61,4 @@ def test_run_function():
         MemorySize=128,
         Publish=True,
     )
-
-    result = client.invoke(
-        FunctionName=FUNCTION_NAME,
-        LogType="Tail",
-    )
-    assert result["StatusCode"] == 200
-    assert result["Payload"].read().decode("utf-8") == "Simple Lambda happy path OK"
+    return client