Created
August 18, 2023 03:46
-
-
Save ikhoon/8518b8346d8928eeb56ad137ca667170 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2023 LINE Corporation | |
* | |
* LINE Corporation licenses this file to you under the Apache License, | |
* version 2.0 (the "License"); you may not use this file except in compliance | |
* with the License. You may obtain a copy of the License at: | |
* | |
* https://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | |
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | |
* License for the specific language governing permissions and limitations | |
* under the License. | |
*/ | |
package com.linecorp.armeria; | |
import org.junit.jupiter.api.Test; | |
import org.junit.jupiter.api.extension.RegisterExtension; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import com.linecorp.armeria.client.WebClient; | |
import com.linecorp.armeria.common.AggregatedHttpResponse; | |
import com.linecorp.armeria.common.FilteredHttpResponse; | |
import com.linecorp.armeria.common.HttpData; | |
import com.linecorp.armeria.common.HttpObject; | |
import com.linecorp.armeria.common.HttpResponse; | |
import com.linecorp.armeria.common.HttpResponseWriter; | |
import com.linecorp.armeria.common.ResponseHeaders; | |
import com.linecorp.armeria.server.ServerBuilder; | |
import com.linecorp.armeria.testing.junit5.server.ServerExtension; | |
class FilterStreamMessageTest { | |
@RegisterExtension | |
static final ServerExtension server = new ServerExtension() { | |
@Override | |
protected void configure(ServerBuilder sb) { | |
sb.service("/", (ctx, req) -> { | |
final HttpResponseWriter streaming = HttpResponse.streaming(); | |
streaming.write(ResponseHeaders.of(200)); | |
streaming.write(HttpData.ofUtf8("{")); | |
streaming.whenConsumed().thenAccept(v -> { | |
streaming.write(HttpData.ofUtf8("\"foo\": \"bar\"")); | |
streaming.whenConsumed().thenAccept(v2 -> { | |
streaming.write(HttpData.ofUtf8("}")); | |
streaming.close(); | |
}); | |
}); | |
return streaming; | |
}); | |
} | |
}; | |
private static final Logger logger = LoggerFactory.getLogger(FilterStreamMessageTest.class); | |
@Test | |
void test() { | |
final WebClient client = | |
WebClient.builder(server.httpUri()) | |
.decorator((delegate, ctx, req) -> { | |
final HttpResponse response = delegate.execute(ctx, req); | |
return new FilteredHttpResponse(response) { | |
@Override | |
protected HttpObject filter(HttpObject obj) { | |
if (obj instanceof HttpData) { | |
logger.debug("data: {}", ((HttpData) obj).toStringUtf8()); | |
} | |
return obj; | |
} | |
}; | |
}) | |
.build(); | |
final AggregatedHttpResponse response = client.get("/").aggregate().join(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment