-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-syntax-highlighting.bru
More file actions
155 lines (128 loc) · 3.89 KB
/
test-syntax-highlighting.bru
File metadata and controls
155 lines (128 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
meta {
name: Bruno Syntax Test
type: http
seq: 1
}
post {
url: https://{{server-url}}:{{port}}/api/endpoint
body: multipartForm
auth: inherit
}
multipart-form {
reqxml: '''
<Request>
<Login>
<Username>{{username}}</Username>
<Password>{{password}}</Password>
</Login>
<Get>
<TestData>
<Parameter>{{test-parameter}}</Parameter>
</TestData>
</Get>
</Request>
'''
}
script:pre-request {
// JavaScript syntax highlighting demonstration
const timestamp = new Date().toISOString();
const randomId = Math.random().toString(36).substring(7);
// Bruno-specific functions
bru.setVar("request-id", randomId);
bru.setVar("timestamp", timestamp);
// Environment variables
const baseUrl = bru.getEnvVar("API_BASE_URL") || "localhost";
// Array and object manipulation
const headers = ["Content-Type", "Authorization", "X-Request-ID"];
const config = {
timeout: 30000,
retries: 3,
validateStatus: (status) => status < 400
};
// Conditional logic
if (baseUrl.includes("prod")) {
console.log("🔴 Production environment detected");
bru.setVar("log-level", "error");
} else {
console.log("🟡 Development environment");
bru.setVar("log-level", "debug");
}
// Regular expressions
const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
const isValidEmail = emailPattern.test("{{username}}");
// Template literals with variables
const apiEndpoint = `${baseUrl}/api/v1/{{resource}}`;
bru.setVar("full-endpoint", apiEndpoint);
}
vars:pre-request {
server-url: localhost
port: 8080
username: admin
password: secret
test-parameter: value123
}
script:post-response {
// Response processing with syntax highlighting
const response = res.getBody();
const status = res.getStatus();
try {
// XML parsing demonstration
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(response, "text/xml");
// Extract values from XML
const statusElement = xmlDoc.querySelector("Status");
if (statusElement) {
const responseStatus = statusElement.textContent;
bru.setVar("api-response-status", responseStatus);
}
// Error handling
const errorElement = xmlDoc.querySelector("Error");
if (errorElement) {
throw new Error(`API Error: ${errorElement.textContent}`);
}
} catch (error) {
console.error("❌ Response parsing failed:", error.message);
bru.setVar("parse-error", error.message);
}
}
tests {
// Test framework syntax highlighting
test("Response status should be 200", function() {
expect(res.getStatus()).to.equal(200);
});
test("Response should contain valid XML", function() {
const body = res.getBody();
expect(body).to.include("<Response>");
expect(body).to.not.include("<Error>");
});
test("Variables should be set correctly", function() {
const requestId = bru.getVar("request-id");
expect(requestId).to.be.a("string");
expect(requestId).to.have.lengthOf(7);
});
}
settings {
encodeUrl: true
}
docs {
# Bruno Syntax Highlighting Test
This file demonstrates the **custom syntax highlighting** for Bruno API files:
## Features
- JSON-like base syntax
- **XML highlighting** in triple-quoted strings
- **JavaScript highlighting** in script blocks
- **Variable highlighting** with `{{variable}}` syntax
- **Markdown highlighting** in docs blocks
### JavaScript Blocks
- `script:pre-request {}` - Pre-request JavaScript execution
- `script:post-response {}` - Response processing
- `tests {}` - Test assertions
### HTTP Methods
- `get`, `post`, `put`, `delete`, etc.
### Special Blocks
- `meta {}` - File metadata
- `multipart-form {}` - Form data with embedded XML
- `vars:pre-request {}` - Variable definitions
- `settings {}` - Request settings
- `docs {}` - Documentation (this block!)
}