Skip to content

Commit ef9f14b

Browse files
committed
Support query params on DELETE requests, closes grpc-ecosystem#29
1 parent 4a3d7af commit ef9f14b

File tree

6 files changed

+68
-48
lines changed

6 files changed

+68
-48
lines changed

generator/template.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func renderURL(r *registry.Registry) func(method data.Method) string {
9696
}
9797
urlPathParams := fmt.Sprintf("[%s]", strings.Join(fieldsInPath, ", "))
9898

99-
if !method.ClientStreaming && method.HTTPMethod == "GET" {
99+
if !method.ClientStreaming && (method.HTTPMethod == "GET" || method.HTTPMethod == "DELETE") {
100100
// parse the url to check for query string
101101
parsedURL, err := url.Parse(methodURL)
102102
if err != nil {

generator/template_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ func TestFieldName(t *testing.T) {
2424
{useProtoNames: false, input: "foo3_bar", want: "foo3Bar"},
2525
{useProtoNames: false, input: "foo_3bar", want: "foo3bar"},
2626
{useProtoNames: false, input: "foo_3_bar", want: "foo3Bar"},
27+
28+
{useProtoNames: true, input: "k8s_field", want: "k8s_field"},
29+
{useProtoNames: true, input: "foo_bar", want: "foo_bar"},
2730
}
2831
for _, tt := range tests {
2932
t.Run(tt.input, func(t *testing.T) {

test/integration/defaultConfig/defaultConfig_test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,14 @@ describe("test default configuration", () => {
100100
expect(result).to.be.empty;
101101
});
102102

103+
it("http delete with query params", async () => {
104+
const result = await CounterService.HTTPDeleteWithParams(
105+
{ id: 10, reason: "test" },
106+
{ pathPrefix: "http://localhost:8081" }
107+
);
108+
expect(result.reason).to.be.equal("test");
109+
});
110+
103111
it("http get request with url search parameters", async () => {
104112
const result = await CounterService.HTTPGetWithURLSearchParams(
105113
{ a: 10, b: { b: 0 }, c: [23, 25], d: { d: 12 } },

test/integration/noStaticClasses/noStaticClasses_test.ts

Lines changed: 38 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,14 @@ import { b64Decode } from "./fetch.pb";
1010

1111
describe("test static functions", () => {
1212
it("unary request", async () => {
13-
const result = await increment(
14-
{ counter: 199 },
15-
{ pathPrefix: "http://localhost:8081" }
16-
);
13+
const result = await increment({ counter: 199 });
1714

1815
expect(result.result).to.equal(200);
1916
});
2017

2118
it("failing unary request", async () => {
2219
try {
23-
await failingIncrement(
24-
{ counter: 199 },
25-
{ pathPrefix: "http://localhost:8081" }
26-
);
20+
await failingIncrement({ counter: 199 });
2721
expect.fail("expected call to throw");
2822
} catch (e) {
2923
expect(e).to.have.property("message", "this increment does not work");
@@ -33,10 +27,8 @@ describe("test static functions", () => {
3327

3428
it("streaming request", async () => {
3529
const response = [] as number[];
36-
await streamingIncrements(
37-
{ counter: 1 },
38-
(resp) => response.push(resp.result),
39-
{ pathPrefix: "http://localhost:8081" }
30+
await streamingIncrements({ counter: 1 }, (resp) =>
31+
response.push(resp.result)
4032
);
4133
expect(response).to.deep.equal([2, 3, 4, 5, 6]);
4234
});
@@ -67,65 +59,67 @@ describe("test with client class", () => {
6759

6860
it("http get check request", async () => {
6961
const req = { numToIncrease: 10 } as HttpGetRequest;
70-
const result = await client.httpGet(req, {
71-
pathPrefix: "http://localhost:8081",
72-
});
62+
const result = await client.httpGet(req);
7363
expect(result.result).to.equal(11);
7464
});
7565

7666
it("http post body check request with nested body path", async () => {
77-
const result = await client.httpPostWithNestedBodyPath(
78-
{ a: 10, req: { b: 15 }, c: 0 },
79-
{ pathPrefix: "http://localhost:8081" }
80-
);
67+
const result = await client.httpPostWithNestedBodyPath({
68+
a: 10,
69+
req: { b: 15 },
70+
c: 0,
71+
});
8172
expect(result.postResult).to.equal(25);
8273
});
8374

8475
it("http post body check request with star in path", async () => {
85-
const result = await client.httpPostWithStarBodyPath(
86-
{ a: 10, req: { b: 15 }, c: 23 },
87-
{ pathPrefix: "http://localhost:8081" }
88-
);
76+
const result = await client.httpPostWithStarBodyPath({
77+
a: 10,
78+
req: { b: 15 },
79+
c: 23,
80+
});
8981
expect(result.postResult).to.equal(48);
9082
});
9183

9284
it("able to communicate with external message reference without package defined", async () => {
93-
const result = await client.externalMessage(
94-
{ content: "hello" },
95-
{ pathPrefix: "http://localhost:8081" }
96-
);
85+
const result = await client.externalMessage({ content: "hello" });
9786
expect(result.result).to.equal("hello!!");
9887
});
9988

10089
it("http patch request with star in path", async () => {
101-
const result = await client.httpPatch(
102-
{ a: 10, c: 23 },
103-
{ pathPrefix: "http://localhost:8081" }
104-
);
90+
const result = await client.httpPatch({ a: 10, c: 23 });
10591
expect(result.patchResult).to.equal(33);
10692
});
10793

10894
it("http delete check request", async () => {
109-
const result = await client.httpDelete(
110-
{ a: 10 },
111-
{ pathPrefix: "http://localhost:8081" }
112-
);
95+
const result = await client.httpDelete({ a: 10 });
11396
expect(result).to.be.empty;
11497
});
11598

99+
it("http delete with query params", async () => {
100+
const result = await client.httpDeleteWithParams({
101+
id: 10,
102+
reason: "test",
103+
});
104+
expect(result.reason).to.be.equal("test");
105+
});
106+
116107
it("http get request with url search parameters", async () => {
117-
const result = await client.httpGetWithURLSearchParams(
118-
{ a: 10, b: { b: 0 }, c: [23, 25], d: { d: 12 } },
119-
{ pathPrefix: "http://localhost:8081" }
120-
);
108+
const result = await client.httpGetWithURLSearchParams({
109+
a: 10,
110+
b: { b: 0 },
111+
c: [23, 25],
112+
d: { d: 12 },
113+
});
121114
expect(result.urlSearchParamsResult).to.equal(70);
122115
});
123116

124117
it("http get request with zero value url search parameters", async () => {
125-
const result = await client.httpGetWithZeroValueURLSearchParams(
126-
{ a: "A", b: "", c: { c: 1, d: [1, 0, 2], e: false } },
127-
{ pathPrefix: "http://localhost:8081" }
128-
);
118+
const result = await client.httpGetWithZeroValueURLSearchParams({
119+
a: "A",
120+
b: "",
121+
c: { c: 1, d: [1, 0, 2], e: false },
122+
});
129123
expect(result).to.deep.equal({
130124
a: "A",
131125
b: "hello",
@@ -134,10 +128,7 @@ describe("test with client class", () => {
134128
});
135129

136130
it("http get request with optional fields", async () => {
137-
const result = await client.httpGetWithOptionalFields(
138-
{},
139-
{ pathPrefix: "http://localhost:8081" }
140-
);
131+
const result = await client.httpGetWithOptionalFields({});
141132

142133
expect(result).to.deep.equal({
143134
// all empty fields will be excluded.

test/integration/protos/service.proto

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,15 @@ message HttpDeleteRequest {
7777
int32 a = 1;
7878
}
7979

80+
message HttpDeleteWithParamsRequest {
81+
int32 id = 1;
82+
string reason = 2;
83+
}
84+
85+
message HttpDeleteWithParamsResponse {
86+
string reason = 1;
87+
}
88+
8089
message HTTPGetWithURLSearchParamsRequest {
8190
int32 a = 1;
8291
PostRequest b = 2;
@@ -200,6 +209,11 @@ service CounterService {
200209
delete: "/delete/{a}"
201210
};
202211
}
212+
rpc HTTPDeleteWithParams(HttpDeleteWithParamsRequest) returns (HttpDeleteWithParamsResponse) {
213+
option (google.api.http) = {
214+
delete: "/delete/{id}"
215+
};
216+
}
203217
rpc ExternalMessage(ExternalRequest) returns (ExternalResponse);
204218
rpc HTTPGetWithURLSearchParams(HTTPGetWithURLSearchParamsRequest) returns (HTTPGetWithURLSearchParamsResponse) {
205219
option (google.api.http) = {

test/integration/service.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ func (r *RealCounterService) HTTPDelete(ctx context.Context, req *HttpDeleteRequ
8282
return &emptypb.Empty{}, nil
8383
}
8484

85+
func (r *RealCounterService) HTTPDeleteWithParams(ctx context.Context, req *HttpDeleteWithParamsRequest) (*HttpDeleteWithParamsResponse, error) {
86+
return &HttpDeleteWithParamsResponse{Reason: req.Reason}, nil
87+
}
88+
8589
func (r *RealCounterService) HTTPGetWithURLSearchParams(ctx context.Context, in *HTTPGetWithURLSearchParamsRequest) (*HTTPGetWithURLSearchParamsResponse, error) {
8690
totalC := 0
8791
for _, c := range in.GetC() {

0 commit comments

Comments
 (0)