Why Observable.fromIterable miss some data in some case? - rx-java2

I write a sample to test flatMap operator in RxJava2, below is the code snippet:
Observable<Integer> observable = ObservableCreate.create(new ObservableOnSubscribe<Integer>() {
#Override
public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
emitter.onNext(1);
emitter.onNext(2);
emitter.onNext(3);
emitter.onNext(4);
emitter.onComplete();
}
});
Disposable disposable = observable.flatMap(new Function<Integer, ObservableSource<String>>() {
#Override
public ObservableSource<String> apply(Integer i) throws Exception {
List<String> list = new ArrayList<>(3);
for(int j = 0; j < 3; j++) {
list.add("I am from apply:" + i);
//list.add("I am from apply:" + i + "_" + j);
}
log("apply: " + listToString(list));
return Observable.fromIterable(list);
}
})
.flatMap(new Function<String, ObservableSource<String>>() {
#Override
public ObservableSource<String> apply(String s) throws Exception {
return Observable.create(new ObservableOnSubscribe<String>() {
#Override
public void subscribe(ObservableEmitter<String> emitter) throws Exception {
emitter.onNext("flatMap--" + s);
emitter.onComplete();
}
});
}
})
.subscribe(
s -> log("accept: " + s)
, throwable -> log(throwable.getMessage())
, () -> log("complete!")
);
Do you think what logs will print? Is it going to something like the following?
apply: [I am from apply:1, I am from apply:1, I am from apply:1]
accept: flatMap--I am from apply:1
accept: flatMap--I am from apply:1
accept: flatMap--I am from apply:1
apply: [I am from apply:2, I am from apply:2, I am from apply:2]
accept: flatMap--I am from apply:2
accept: flatMap--I am from apply:2
accept: flatMap--I am from apply:2
apply: [I am from apply:3, I am from apply:3, I am from apply:3]
accept: flatMap--I am from apply:3
accept: flatMap--I am from apply:3
accept: flatMap--I am from apply:3
apply: [I am from apply:4, I am from apply:4, I am from apply:4]
accept: flatMap--I am from apply:4
accept: flatMap--I am from apply:4
accept: flatMap--I am from apply:4
complete!
I also think that I'll get these logs, but I am wrong, I only get two for each string, just like this:
apply: [I am from apply:1, I am from apply:1, I am from apply:1]
accept: flatMap--I am from apply:1
accept: flatMap--I am from apply:1
apply: [I am from apply:2, I am from apply:2, I am from apply:2]
accept: flatMap--I am from apply:2
accept: flatMap--I am from apply:2
apply: [I am from apply:3, I am from apply:3, I am from apply:3]
accept: flatMap--I am from apply:3
accept: flatMap--I am from apply:3
apply: [I am from apply:4, I am from apply:4, I am from apply:4]
accept: flatMap--I am from apply:4
accept: flatMap--I am from apply:4
complete!
I try to find the reason of this, so I add some breakpoints and debug step in step, unfortunately, I get the right result after debugging. I am so confused, does fromIterable operator miss some data in some case?

Related

flutter-Bad response 'Sec-WebSocket-Accept' header

In many test pages you can request success, but in flutter throw the error “Bad response 'Sec-WebSocket-Accept' header”
void initWebSocket(){
String path = "***";
Future future = WebSocket.connect(path).then((wb){
_webSocket = wb;
wb.listen((event) {
print("服务端---->:"+event);
},
onError:(err){
print("服务端----error--->:"+err);
},
onDone: (){
print("服务端----onDone");
});
});
}
Response:
Hypertext Transfer Protocol GET / HTTP/1.1\r\n user-agent: Dart/2.12 (dart:io)\r\n connection: Upgrade\r\n cache-control: no-cache\r\n accept-encoding: gzip\r\n content-length: 0\r\n sec-websocket-version: 13\r\n host: **\r\n sec-websocket-extensions: permessage-deflate; client_max_window_bits\r\n sec-websocket-key: rtbsyWoIQrZMpr1v9rN1Dw==\r\n upgrade: websocket\r\n \r\n [Full request URI: http://**/] [HTTP request 1/1] [Response in frame: 81]

Unable to open "File" or "Edit" menu in Notepad using WinAppDriver

I'm trying to learn how to use WinAppDriver to test my desktop app developed in VS2019.
I'm able to run a NUnit test using the basic Notepad.exe and example of typing text. However, I'm unable to click on menu elements in Notepad for example. How do you achieve this?
Inspect.exe says its name is "Edit".
I'm using the Appium.WebDriver 4.1.1 nuget package.
Here is a good example of what I'm trying to do, but the code is outdated: https://github.com/microsoft/WinAppDriver/blob/master/Samples/C%23/NotepadTest/ScenarioMenuItem.cs
Test Code in VS2019
using NUnit.Framework;
using OpenQA.Selenium.Appium.Windows;
using System;
namespace Notepad_Tests
{
public class Tests
{
protected static WindowsDriver<WindowsElement> NotepadSession;
[SetUp]
public void Setup()
{
var appiumOptions = new OpenQA.Selenium.Appium.AppiumOptions();
appiumOptions.AddAdditionalCapability("platformName", #"Windows");
appiumOptions.AddAdditionalCapability("deviceName", #"WindowsPC");
appiumOptions.AddAdditionalCapability("app", #"C:\Windows\notepad.exe");
appiumOptions.AddAdditionalCapability("app", #"C:\Windows\notepad.exe");
appiumOptions.AddAdditionalCapability("appArguments", #"MyTestFile.txt");
appiumOptions.AddAdditionalCapability("appWorkingDir", #"D:\MyTestFolder\");
NotepadSession = new WindowsDriver<WindowsElement>(new Uri("http://127.0.0.1:4723"), appiumOptions);
}
[Test]
public void Test1()
{
NotepadSession.FindElementByClassName("Edit").SendKeys("This is some text");
NotepadSession.FindElementByName("Edit").Click();
}
[TearDown]
public void TestCleanup()
{
// NotepadSession.Quit();
}
}
}
Error in WinAppDriver.exe window
==========================================
POST /session HTTP/1.1
Accept: application/json, image/png
Connection: Keep-Alive
Content-Length: 236
Content-Type: application/json; charset=utf-8
Host: 127.0.0.1:4723
User-Agent: selenium/3.141.0 (.net windows)
{"desiredCapabilities":{"platformName":"Windows","deviceName":"WindowsPC","app":"C:\\Windows\\notepad.exe","appArguments":"MyTestFile.txt","appWorkingDir":"D:\\MyTestFolder\\"},"capabilities":{"firstMatch":[{"platformName":"Windows"}]}}
HTTP/1.1 200 OK
Content-Length: 200
Content-Type: application/json
{"sessionId":"431F0707-71BD-4645-B085-5BD750FC2067","status":0,"value":{"app":"C:\\Windows\\notepad.exe","appArguments":"MyTestFile.txt","appWorkingDir":"D:\\MyTestFolder\\","platformName":"Windows"}}
==========================================
POST /session/431F0707-71BD-4645-B085-5BD750FC2067/element HTTP/1.1
Accept: application/json, image/png
Connection: Keep-Alive
Content-Length: 37
Content-Type: application/json; charset=utf-8
Host: 127.0.0.1:4723
User-Agent: selenium/3.141.0 (.net windows)
{"using":"class name","value":"Edit"}
HTTP/1.1 200 OK
Content-Length: 96
Content-Type: application/json
{"sessionId":"431F0707-71BD-4645-B085-5BD750FC2067","status":0,"value":{"ELEMENT":"42.2099874"}}
==========================================
POST /session/431F0707-71BD-4645-B085-5BD750FC2067/element/42.2099874/value HTTP/1.1
Accept: application/json, image/png
Connection: Keep-Alive
Content-Length: 31
Content-Type: application/json; charset=utf-8
Host: 127.0.0.1:4723
User-Agent: selenium/3.141.0 (.net windows)
{"value":["This is some text"]}
HTTP/1.1 200 OK
Content-Length: 63
Content-Type: application/json
{"sessionId":"431F0707-71BD-4645-B085-5BD750FC2067","status":0}
==========================================
POST /session/431F0707-71BD-4645-B085-5BD750FC2067/element HTTP/1.1
Accept: application/json, image/png
Connection: Keep-Alive
Content-Length: 31
Content-Type: application/json; charset=utf-8
Host: 127.0.0.1:4723
User-Agent: selenium/3.141.0 (.net windows)
{"using":"name","value":"Edit"}
HTTP/1.1 200 OK
Content-Length: 128
Content-Type: application/json
{"sessionId":"431F0707-71BD-4645-B085-5BD750FC2067","status":0,"value":{"ELEMENT":"42.3148312.3.-2147483646.27904.691604525.2"}}
==========================================
POST /session/431F0707-71BD-4645-B085-5BD750FC2067/element/42.3148312.3.-2147483646.27904.691604525.2/click HTTP/1.1
Accept: application/json, image/png
Connection: Keep-Alive
Content-Length: 2
Content-Type: application/json; charset=utf-8
Host: 127.0.0.1:4723
User-Agent: selenium/3.141.0 (.net windows)
{}
HTTP/1.1 400 Bad Request
Content-Length: 175
Content-Type: application/json
{"status":105,"value":{"error":"element not interactable","message":"An element command could not be completed because the element is not pointer- or keyboard interactable."}}
I got it to work by using the Microsoft.WinAppDriver.Appium.WebDriver Prerlease nugget package instead of the Appium.WebDriver 4.1.1 nuget package.
Then, the example here works perfectly:
https://github.com/microsoft/WinAppDriver/blob/master/Samples/C%23/NotepadTest/ScenarioMenuItem.cs

NodeSeqMarshaller with ContentType charset not resolving

Attempting to get a spray endpoint negotiating a postman request for content. My XML marshaller seems to let me down, ie, it never resolves the correct marshaller option based on Accept header and charset.
I have the following:
object ResponseVO {
val NodeSeqMarshaller = Marshaller.delegate[ResponseVO, NodeSeq](ContentType(`text/xml`, `UTF-8`)) { respVO => <ussd><type>{ respVO.respType }</type><message>{ respVO.message }</message></ussd> }
val supportedContentTypes = List[ContentType](ContentType(`text/xml`, `UTF-8`))
implicit val marshaller = Marshaller[ResponseVO] { (respVO, ctx) =>
ctx.tryAccept(supportedContentTypes) match {
case Some(ContentType(`text/xml`, `UTF-8`)) => NodeSeqMarshaller(respVO, ctx)
case whatever => println(whatever); ctx.rejectMarshalling(supportedContentTypes);
}
}
}
and the following route:
trait USSDRoute {
this: SimpleRoutingApp with BootStrappedActorSystem =>
val ussdRoute = path("ussd") {
parameters('msisdn.as[Long], 'session.as[String], 'type.as[Int], 'msg.as[String], 'network.as[Int]) { (msisdn, session, reqType, msg, network) =>
complete {
val reqVO = RequestVO(msisdn, session, reqType, msg, network)
println(s"received $reqVO")
ResponseVO(1, "Spirit midget medium escapes from prison, headlines read: Small medium at large!")
}
}
}
}
Unfortunately I never seem to negotiate correctly, ie, my marshaller resolution will dive bomb into the "whatever" block and respond with a 406
"Resource representation is only available with these Content-Types:
text/xml; charset=UTF-8"
I am using Postman and my request headers read:
GET /ussd?msisdn=0794138690&type=1&network=4&msg=hello&session=99 HTTP/1.1
Host: localhost:9999
Connection: keep-alive
Accept: text/xml; charset=UTF-8
Cache-Control: no-cache
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML,
like Gecko) Chrome/40.0.2214.91 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8,nl;q=0.6
It's probably something small and silly - hopefully someone can help steer me in the right direction.
Thank you
Have a look at how the case class ContentType is defined:
case class ContentType(mediaType: MediaType, definedCharset: Option[HttpCharset])
Note that definedCharset is defined as an Option[HttpCharset], therefore your pattern match via
case Some(ContentType(`text/xml`, `UTF-8`)) => ...
never can succeed. So you need to use a Some to perform the pattern match successfully:
case Some(ContentType(`text/xml`, Some(`UTF-8`))) => ...

Enabling CORS for Cowboy REST API

How can I enable CORS for cowboy rest handler?
I tried to add options/2 method, like this:
options(Req, State) ->
{[
{<<"access-control-allow-origin">>, <<"*">>},
{<<"access-control-allow-methods">>, <<"GET, OPTIONS">>}
], Req, State}.
but this causes errors like:
Error in process <0.263.0> with exit value: {{case_clause,{[{<<27 bytes>>,<<1 byte>>},{<<28 bytes>>,<<12 bytes>>}],{http_req,#Port<0.2636>,ranch_tcp,keepalive,<0.263.0>,<<7 bytes>>,{1,1},{{127,0,0,1},56522},<<9 bytes>>,undefined,9090,<<8 bytes>>,undefined,<<0 bytes>>,undefined,<<0 bytes>>,[],[{<<4 bytes>>,<<14 bytes>>},{<<10 bytes>>,<<74 bytes>>},{<<6 bytes>>,<<63 bytes>>},{<<15 bytes>>,<<14 bytes>>},{<<15 bytes>>,<<13 bytes>>},{<<6 bytes>>,<<4 bytes>>},{<<29 bytes>>,<<3 bytes>>},{<<30 bytes>>,<<16 bytes>>},{<<10 bytes>>,<<10 bytes>>}],[{<<10 bytes>>,[<<10 bytes>>]}],undefined,[],waiting,undefined,<<0 bytes>>,false,waiting,[],<<0 bytes>>,undefined},undefined...
Where is my mistake?
Cowboy documentation says you need to set header by using set_resp_headers, not return a list of headers:
%% If you need to add additional headers to the response at this point,
%% you should do it directly in the options/2 call using set_resp_headers.
So your code should look like:
options(Req, State) ->
Req1 = cowboy_req:set_resp_header(<<"access-control-allow-methods">>, <<"GET, OPTIONS">>, Req),
Req2 = cowboy_req:set_resp_header(<<"access-control-allow-origin">>, <<"*">>, Req1),
{ok, Req2, State}.
You can test with
curl -H "Origin: http://example.com" \
-H "Access-Control-Request-Method: GET" \
-H "Access-Control-Request-Headers: X-Requested-With" \
-X OPTIONS --verbose \
http://localhost:8080
* About to connect() to localhost port 8080 (#0)
* Trying 127.0.0.1... connected
* Connected to localhost (127.0.0.1) port 8080 (#0)
> OPTIONS / HTTP/1.1
> User-Agent: curl/7.21.4 (universal-apple-darwin11.0) libcurl/7.21.4 OpenSSL/0.9.8r zlib/1.2.5
> Host: localhost:8080
> Accept: */*
> Origin: http://example.com
> Access-Control-Request-Method: GET
> Access-Control-Request-Headers: X-Requested-With
>
< HTTP/1.1 200 OK
< connection: keep-alive
< server: Cowboy
< date: Mon, 25 Mar 2013 15:59:11 GMT
< content-length: 0
< access-control-allow-methods: GET, OPTIONS
< access-control-allow-origin: *
<
* Connection #0 to host localhost left intact
* Closing connection #0
My REST allows acceess to resources only and only if token is provided. So I had to implement is_authorized/2 callback which is different for OPTIONS and GET requests. See below:
1) Implement options/2 as follows
options(Req0, State) ->
% cors
Req1 = cowboy_req:set_resp_header(
<<"access-control-allow-methods">>, <<"GET, OPTIONS">>, Req0),
Req2 = cowboy_req:set_resp_header(
<<"access-control-allow-origin">>, <<"*">>, Req1),
Req3 = cowboy_req:set_resp_header(
<<"access-control-allow-headers">>, <<"authorization">>, Req2),
{ok, Req3, State}.
2) Implement is_authorized/2 as follows
is_authorized(Req, State) ->
case cowboy_req:method(Req) of
<<"GET">> ->
case cowboy_req:parse_header(<<"authorization">>, Req) of
{bearer, <<Token/binary>>} ->
{true, Req, Token};
_ ->
{{false, <<"Basic realm=\"cowboy\"">>}, Req, State}
end;
<<"OPTIONS">> ->
{true, Req, State}
end.
3) to_json/2 method sending access-control-allow-origin
to_json(Req0, State) ->
R = #{ foo => [<<"bar">>] },
% cors
Req1 = cowboy_req:set_resp_header(
<<"access-control-allow-origin">>, <<"*">>, Req0),
Body = jsx:encode(R),
{Body, Req1, State}.
You can try this:
Req1 = cowboy_req:set_resp_header(<<"access-control-allow-origin">>, <<"*">>, Req0 -> Worked for me.
In addition to the answers found here, Cowboy 2.* appears to have a middleware feature which you can use to intercept all REST handler requests to a server, and add the required response headers:
https://ninenines.eu/docs/en/cowboy/2.7/guide/middlewares/
An example CORS middleware implementation can be found in this Github gist:
https://github.com/ninenines/cowboy/blob/master/examples/markdown_middleware/src/markdown_middleware_app.erl

Passing parameters to WCF restful service with POST method

how can I use Fiddler to pass 2 parameters to a method with a signature like this :
[WebInvoke(UriTemplate = "Login", Method = "POST", ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
bool Login(string login, string password);
Any suggestion to change something in the method is welcome, but I have always to pass 2 string parameters.
It is much easier to the services with WCFTestClient. Anyway, Fiddler has composer functionality which can be used to test any http calls.
Request Headers should look like below. Just change SOAPAction accordingly.
Content-Type: text/xml; charset=utf-8
SOAPAction: "http://tempuri.org/IService/Login"
Host: localhost:2045
Content-Length: 179
Expect: 100-continue
Accept-Encoding: gzip, deflate
Proxy-Connection: Keep-Alive
And request body:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><Login xmlns="http://tempuri.org/"><login>sdf</login><password>sdf</password></Login></s:Body></s:Envelope>