Skip to content

Commit

Permalink
gimmehttp - updated to use better json builder for languages
Browse files Browse the repository at this point in the history
  • Loading branch information
brianvoe committed Jan 8, 2025
1 parent 7e1247d commit ee5bea4
Show file tree
Hide file tree
Showing 40 changed files with 1,255 additions and 520 deletions.
80 changes: 72 additions & 8 deletions src/gimmeHTTP/clients/c.libcurl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,47 @@ int main(void) {
fprintf(stderr, "failed: %s", curl_easy_strerror(res));
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return 0;
}
`.trim()
)
})

test('should build a GET request with cookies', () => {
const httpRequest: Http = {
method: 'GET',
url: 'https://example.com',
cookies: {
session: 'abc123',
user: 'testuser'
}
}
const config: Config = {}
const result = CLibCurl.generate(config, httpRequest)
expect(result).toBe(
`
#include <stdio.h>
#include <curl/curl.h>
int main(void) {
CURL *curl;
CURLcode res;
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "https://example.com");
curl_easy_setopt(curl, CURLOPT_COOKIE, "session=abc123; user=testuser");
res = curl_easy_perform(curl);
if(res != CURLE_OK)
fprintf(stderr, "failed: %s", curl_easy_strerror(res));
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return 0;
}
Expand Down Expand Up @@ -73,14 +114,15 @@ int main(void) {
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return 0;
}
`.trim()
)
})

test('should build a POST request with body', () => {
test('should build a POST request with json body', () => {
const httpRequest: Http = {
method: 'POST',
url: 'https://example.com',
Expand Down Expand Up @@ -114,23 +156,30 @@ int main(void) {
fprintf(stderr, "failed: %s", curl_easy_strerror(res));
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return 0;
}
`.trim()
)
})

test('should build a GET request with cookies', () => {
test('should build a POST request with advanced json body', () => {
const httpRequest: Http = {
method: 'GET',
method: 'POST',
url: 'https://example.com',
cookies: {
session: 'abc123',
user: 'testuser'
body: {
key1: 'value1',
key2: 123,
key3: true,
key4: [1, 2, 3],
key5: {
nested: 'value'
},
key6: null
}
}
const config: Config = {}
const config: Config = { indent: ' ' }
const result = CLibCurl.generate(config, httpRequest)
expect(result).toBe(
`
Expand All @@ -145,14 +194,29 @@ int main(void) {
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "https://example.com");
curl_easy_setopt(curl, CURLOPT_POST, 1L);
curl_easy_setopt(curl, CURLOPT_COOKIE, "session=abc123; user=testuser");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, R"({
"key1": "value1",
"key2": 123,
"key3": true,
"key4": [
1,
2,
3
],
"key5": {
"nested": "value"
},
"key6": null
})");
res = curl_easy_perform(curl);
if(res != CURLE_OK)
fprintf(stderr, "failed: %s", curl_easy_strerror(res));
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return 0;
}
Expand Down
17 changes: 2 additions & 15 deletions src/gimmeHTTP/clients/c.libcurl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export default {
if (http.body) {
builder.line()
builder.line('curl_easy_setopt(curl, CURLOPT_POSTFIELDS, R"(')
formatJsonBody(http.body, builder)
builder.json(http.body)
builder.append(')");')
}

Expand All @@ -73,6 +73,7 @@ export default {
builder.line('curl_easy_cleanup(curl);')
builder.outdent()
builder.line('}')
builder.line()
builder.line('curl_global_cleanup();')
builder.line('return 0;')
builder.outdent()
Expand All @@ -81,17 +82,3 @@ export default {
return builder.output()
}
} as Client

function formatJsonBody(json: any, builder: Builder): void {
const lines = JSON.stringify(json, null, 2).split('\n')
// for loop with indea and value
for (let i = 0; i < lines.length; i++) {
// if first line, append instead of line
if (i === 0) {
builder.append(`${lines[i]}`)
continue
}

builder.line(`${lines[i]}`)
}
}
71 changes: 63 additions & 8 deletions src/gimmeHTTP/clients/csharp.http.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,47 @@ namespace HttpClientExample
)
})

test('should build a GET request with cookies', () => {
const httpRequest: Http = {
method: 'GET',
url: 'https://example.com',
cookies: {
session: 'abc123',
user: 'testuser'
}
}
const config: Config = {}
const result = CSharpHttp.generate(config, httpRequest)
expect(result).toBe(
`
using System;
using System.Net.Http;
using System.Threading.Tasks;
namespace HttpClientExample
{
class Program
{
static async Task Main(string[] args)
{
using (HttpClient client = new HttpClient())
{
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.GET, "https://example.com");
request.Headers.Add("Cookie", "session=abc123; user=testuser");
HttpResponseMessage response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
}
}
}
}
`.trim()
)
})

test('should build a POST request with body', () => {
const httpRequest: Http = {
method: 'POST',
Expand Down Expand Up @@ -122,13 +163,16 @@ namespace HttpClientExample
)
})

test('should build a GET request with cookies', () => {
test('should build a POST with advanced request json body', () => {
const httpRequest: Http = {
method: 'GET',
method: 'POST',
url: 'https://example.com',
cookies: {
session: 'abc123',
user: 'testuser'
body: {
key1: 'value1',
key2: 123,
key3: true,
key4: ['value1', 'value2'],
key5: { nestedKey: 'nestedValue' }
}
}
const config: Config = {}
Expand All @@ -147,9 +191,20 @@ namespace HttpClientExample
{
using (HttpClient client = new HttpClient())
{
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.GET, "https://example.com");
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.POST, "https://example.com");
request.Headers.Add("Cookie", "session=abc123; user=testuser");
request.Content = new StringContent({
"key1": "value1",
"key2": 123,
"key3": true,
"key4": [
"value1",
"value2"
],
"key5": {
"nestedKey": "nestedValue"
}
}, System.Text.Encoding.UTF8, "application/json");
HttpResponseMessage response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
Expand All @@ -159,7 +214,7 @@ namespace HttpClientExample
}
}
}
`.trim()
`.trim()
)
})
})
22 changes: 1 addition & 21 deletions src/gimmeHTTP/clients/csharp.http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ export default {
if (http.body) {
builder.line()
builder.line('request.Content = new StringContent(')
builder.indent()
buildJsonBody(builder, http.body)
builder.json(http.body)
builder.append(', System.Text.Encoding.UTF8, "application/json");')
}

Expand All @@ -77,22 +76,3 @@ export default {
return builder.output()
}
} as Client

function buildJsonBody(builder: Builder, body: any): void {
const lines = JSON.stringify(body, null, builder.getIndent()).split('\n')
for (let i = 0; i < lines.length; i++) {
const line = lines[i].trim()

if (i === 0) {
builder.append(line)
continue
}

// If last line, outdent
if (i === lines.length - 1) {
builder.outdent()
}

builder.line(line)
}
}
Loading

0 comments on commit ee5bea4

Please sign in to comment.