Skip to content

Commit f7ab11e

Browse files
docs(srv): update local testing functions doc MTA-5558 (#4400)
* docs(srv): update local testing functions doc MTA-5558 * docs(srv): update
1 parent 3042fbb commit f7ab11e

File tree

1 file changed

+269
-12
lines changed

1 file changed

+269
-12
lines changed

pages/serverless-functions/reference-content/local-testing.mdx

Lines changed: 269 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,288 @@ content:
77
paragraph: Learn how to test your Serverless Functions locally before deployment on Scaleway.
88
tags: functions serverless local testing
99
dates:
10-
validation: 2024-10-03
10+
validation: 2025-02-11
1111
posted: 2023-03-06
1212
categories:
1313
- serverless
1414
---
1515

1616
Local testing allows you to run your code on your development environment with a short deploy time.
1717

18-
Available frameworks:
19-
20-
- [NodeJS](https://github.com/scaleway/serverless-functions-node#%EF%B8%8F-quickstart)
21-
- [Python](https://github.com/scaleway/serverless-functions-python#%EF%B8%8F-quickstart)
22-
- [Go](https://github.com/scaleway/serverless-functions-go#%EF%B8%8F-quickstart)
23-
24-
<Message type="tip">
25-
Our real-world examples use local testing. Check our [serverless-examples repository](https://github.com/scaleway/serverless-examples) for more information.
26-
</Message>
27-
28-
# Local testing
2918

3019
Scaleway Serverless Functions can run outside Scaleway infrastructures. This is useful to set up, develop, and test functions locally. You can run functions directly on your machines to debug them and analyze logs.
3120

3221
Each local testing framework is written in its respective language and emulates the calls made to functions by the Scaleway infrastructure.
3322
The result is a language-specific executable or script that users can connect to with their favorite debugging and logging tools.
3423

24+
## Local testing quickstarts
25+
26+
<Tabs id="frameworks">
27+
28+
<TabsTab label="NodeJS">
29+
30+
Refer to the [NodeJS local testing repository](https://github.com/scaleway/serverless-functions-node) for more information on testing your function locally using Node.
31+
32+
**Quickstart**
33+
34+
1. Install the Scaleway Serverless Functions package using `npm`:
35+
```sh
36+
npm i @scaleway/serverless-functions
37+
```
38+
39+
2. Add the following code to the file containing your handle:
40+
41+
For ES modules
42+
```js
43+
// handler.js
44+
45+
import { pathToFileURL } from "url";
46+
47+
function handle(event, context, callback) {
48+
return {
49+
statusCode: 201,
50+
body: JSON.stringify({
51+
message: "Hello World!",
52+
}),
53+
headers: {
54+
"Content-Type": "application/json",
55+
},
56+
};
57+
}
58+
59+
// This part will execute when testing locally, but not when the function is deployed online
60+
if (import.meta.url === pathToFileURL(process.argv[1]).href) {
61+
import("@scaleway/serverless-functions").then(scw_fnc_node => {
62+
scw_fnc_node.serveHandler(handle, 8080);
63+
});
64+
}
65+
```
66+
67+
For common JS:
68+
```js
69+
// handler.js
70+
71+
const url = require("url");
72+
73+
module.exports.handle = (event, context, callback) => {
74+
return {
75+
statusCode: 201,
76+
body: JSON.stringify({
77+
message: "Hello World!",
78+
}),
79+
headers: {
80+
"Content-Type": "application/json",
81+
},
82+
};
83+
};
84+
85+
// This part will execute when testing locally, but not when the function is deployed online
86+
if ("file://" + __filename === url.pathToFileURL(process.argv[1]).href) {
87+
import("@scaleway/serverless-functions").then(scw_fnc_node => {
88+
scw_fnc_node.serveHandler(exports.handle, 8080);
89+
});
90+
}
91+
```
92+
3. In a terminal, run the command below to execute your file and start the local webserver:
93+
94+
```sh
95+
node handler.js
96+
```
97+
98+
4. In another terminal session, run the command below:
99+
```sh
100+
curl -X GET http://localhost:8080
101+
```
102+
103+
The function returns the content of its body:
104+
105+
```js
106+
{"message":"Hello World!"}%
107+
```
108+
</TabsTab>
109+
110+
<TabsTab label="Python">
111+
112+
Refer to the [Python local testing repository](https://github.com/scaleway/serverless-functions-python) for more information on testing your functions locally using Python.
113+
114+
**Quickstart**
115+
116+
1. Install the Scaleway Serverless Functions package using `pip`:
117+
```sh
118+
pip install scaleway-functions-python
119+
```
120+
121+
2. Add the following code to the file containing your handle:
122+
123+
```python
124+
# handler.py
125+
126+
# Standard entrypoint to a Scaleway serverless function
127+
def handler(event, context):
128+
if event["httpMethod"] != "GET":
129+
return {"statusCode": 405, "body": "Invalid method!"}
130+
return "Hello World!"
131+
132+
if __name__ == "__main__":
133+
# The import is conditional so that you do not need
134+
# to package the library when deploying on Scaleway Functions.
135+
from scaleway_functions_python import local
136+
local.serve_handler(handler, port=8080)
137+
```
138+
139+
3. In a terminal, run the command below to execute your file and start the local webserver:
140+
141+
```sh
142+
python handler.py
143+
```
144+
145+
4. In another terminal session, run the command below:
146+
```sh
147+
curl http://localhost:8080
148+
```
149+
150+
The function returns the expected output:
151+
152+
```python
153+
Hello World!
154+
```
155+
156+
<Message type="note">
157+
The function above only processes GET requests, as declared in its code. Other requests will return the defined error message:
158+
159+
```sh
160+
curl -X POST http://localhost:8080
161+
> Invalid method!
162+
```
163+
164+
</Message>
165+
166+
</TabsTab>
167+
168+
<TabsTab label="Go">
169+
170+
Refer to the [Go local testing repository](https://github.com/scaleway/serverless-functions-go) for more information on testing your functions locally using Go.
171+
172+
**Quickstart**
173+
174+
1. Install the Scaleway Serverless Functions package using `go get`:
175+
```sh
176+
go get github.com/scaleway/serverless-functions-go
177+
```
178+
179+
2. In a new folder, create a file named `handler.go` and add the following code to it:
180+
```go
181+
package handler
182+
183+
import (
184+
"encoding/json"
185+
"net/http"
186+
)
187+
188+
// This handle function comes from our examples and is not modified.
189+
func Handle(w http.ResponseWriter, r *http.Request) {
190+
response := map[string]any{
191+
"message": "We're all good",
192+
"healthy": true,
193+
"number": 4,
194+
"headers": r.Header,
195+
}
196+
197+
responseBytes, err := json.Marshal(response)
198+
if err != nil {
199+
w.WriteHeader(http.StatusInternalServerError)
200+
return
201+
}
202+
203+
// Set the header explicitly depending the returned data
204+
w.Header().Set("Content-Type", "application/json")
205+
206+
// Customize status code
207+
w.WriteHeader(http.StatusOK)
208+
209+
// Add content to the response
210+
_, _ = w.Write(responseBytes)
211+
}
212+
```
213+
214+
3. Create a `main.go` file in a new `cmd` subfolder, then add the following code to it:
215+
216+
```go
217+
package main
218+
219+
import (
220+
// "localfunc" is the module name located in your go.mod. To generate a go.mod with "localfunc" as a name, you can use the following command : go mod init localfunc.
221+
222+
// Otherwise, you can replace "localfunc" with the name of your own module.
223+
localfunc "github.com/scaleway/serverless-functions-go/examples/handler"
224+
"github.com/scaleway/serverless-functions-go/local"
225+
)
226+
227+
func main() {
228+
// Replace "Handle" with your function handler name if necessary
229+
local.ServeHandler(localfunc.Handle, local.WithPort(8080))
230+
}
231+
```
232+
233+
4. Run the commands below to generate a `mod` file, then automatically add the modules to it:
234+
235+
```sh
236+
go mod init localfunc && go mod tidy
237+
```
238+
239+
5. Run the command below to create a new function for local testing:
240+
241+
```sh
242+
go run cmd/main.go
243+
```
244+
245+
6. In another terminal session, run the command below:
246+
```sh
247+
curl http://localhost:8080
248+
```
249+
250+
The function returns the expected output:
251+
252+
```json
253+
{
254+
"headers": {
255+
"Accept": [
256+
"*/*"
257+
],
258+
"Forwarded": [
259+
"for=localhost:8080;proto=http"
260+
],
261+
"K-Proxy-Request": [
262+
"activator"
263+
],
264+
"User-Agent": [
265+
"curl/8.4.0"
266+
],
267+
"X-Envoy-External-Address": [
268+
"localhost:8080"
269+
],
270+
"X-Forwarded-For": [
271+
"localhost:8080",
272+
"127.0.0.1",
273+
"127.0.0.2"
274+
],
275+
"X-Forwarded-Proto": [
276+
"http"
277+
],
278+
"X-Request-Id": [
279+
"5283e1ee-e88e-11ef-ab6e-acde48001122"
280+
]
281+
},
282+
"healthy": true,
283+
"message": "We're all good",
284+
"number": 4
285+
}
286+
```
287+
288+
</TabsTab>
289+
290+
</Tabs>
291+
35292
## Requirements
36293
37294
Testing your function locally will create a local web server that listens to a given port.

0 commit comments

Comments
 (0)