AnWeber / vscode-httpyac

Quickly and easily send REST, Soap, GraphQL, GRPC, MQTT and WebSocket requests directly within Visual Studio Code
https://marketplace.visualstudio.com/items?itemName=anweber.vscode-httpyac
MIT License
222 stars 20 forks source link

Problems with the httpyac.config.js file #290

Closed alekdavisintel closed 1 month ago

alekdavisintel commented 1 month ago

I have a working project (very simple, just a couple of tests). I added an httpyac.config.js file to the project folder and copied the content as-is from the sample at https://httpyac.github.io/config/#httpyac-config-js. The project file structure looks like this:

image

Once I added the config file, I immediately got an error: ReferenceError: models is not defined at Object.<anonymous>. I assumed it corresponded to the level: models.LogLevel.warn setting in the file, so I removed it. The error disappeared, but now the the requests were hanging. I suspected it to be a proxy issue, so I added the proxy setting to the config file, and it resolved the hanging request problem (I assumed that if the setting is not present in the file, the default would be used, but it looks like I was wrong). Now, when I run tests, I get a file not found warning (at the bottom right corner).

image

The test complete without issues, so I'm not sure why the warning pops up, although the warning makes sense because my import statement looks like this :

# @import ../../../common.http

Logically, the import statement should use two levels up, not three, i.e. ../../common.http, but if I change it to use two levels up, none of my variables defined in the common.http file get resolved and the tests fail with the baseUrl is not defined error:

image

Any ideas what is going on here?

P.S. I removed the httpyac.config.js file from the project, bu the file not found warning still shows (the tests still run fine), so I'm not sure if it is config file related (before I started messing with the config file, I did not get the warning, but maybe I did something, while trying different things).

AnWeber commented 1 month ago

I must confess that I can't manage the problems you describe. The ReferenceError is clear because I accidentally wrote Typescript code in Javascript (example is corrected). But then I can't manage the subsequent problems. Requests work for me even without setting the proxy. The inline error with baseUrl not defined does not come from my extension. I did not implement some kind of inline error handling, only popups The File not found warning comes from my extension, but I have decided not to prevent the execution on such an error. If the variables are already present, the requests would still go through. The handling of variables in the VSCode extension is relatively lax to allow easy use. VSCode simply uses all variables that have already been used once before and also caches them between executions. Setting httpyac.regionScopedVariables would change behaviour or reset Environment between runs.

test.zip

alekdavisintel commented 1 month ago

Thank you for the sample. I installed it as-is (although, had to change the host2 property in the environment file to host because with host2, I got an error complaining about the endpoint URL). It ran fine when I was not on the corporate VPN, but as soon as I got on the VPN, it failed because it could not reach the endpoint. I turned VPN off and it worked fine again. Then I turned VPN on again, closed Visual Studio Code, renamed the .httpyac.config.js file to .httpyac.config.js.BAK, relaunched Visual Studio Code, and the request worked (with VPN turned on).

To summarize: when using a config file, if a proxy setting was not explicitly defined in the file, requests over VPN did not work, but without the config file they did. I do not remember if I configured global proxy setting for httpYAC somewhere when I installed it. I do not think so, but maybe I forgot. And what it looks like to me is that when a config file is used, proxy settings are treated differently. It's not a big deal, but a bit weird.

I should mention that our proxy settings are configured in a setup script that is pretty complicated (I do not understand details, but it uses the client and the destination URL to determine if a proxy is needed and, if so, which one, since clients in India would use a different proxy servers from client in Europe or North America). It looks something like this (with corporate specific info removed):

function FindProxyForURL(url,host) {
var me=myIpAddress();
var resolved_ip = dnsResolve(host);
if (host == "127.0.0.1") {return "DIRECT";}
if (host == "localhost") {return "DIRECT";}
if (isPlainHostName(host)) {return "DIRECT";}
if (url.substring(0,37) == "http://abc.def.com") {return "DIRECT";}
if (!resolved_ip) {return "PROXY xxx.xxx.com:XXX";}
// 100 more if statements
// ...
return "PROXY yyy.xxx.com:XXX";
}

I figure out the missing file problem (thanks for the sample again because it helped my get on the right path). I had too many up levels in my import statement, so once I changed @import ../../../common.http to @import ../common.http the warning disappeared. I am surpised it worked at all with ../../.., so let's consider it a miracle. :-)

Thank you so much for all your help. I just love your extension. It is a pleasure to use (although, I am still hazy about some areas, but I will try to figure them out). I originally wanted to find something that would understand our original Postman implementation, but after working with httpYAC for a couple of days, I like its architecture a lot better, so I do not mind doing a conversion. Once I finish my POC, assuming I will not run into any show stoppers, I will try to push httpYAC for adoption across the IT and other organizations.

Thanks again. I really appreciate your work and all the help.

AnWeber commented 1 month ago

The different behavior irritates me. It should not be due to the proxy PAC. I didn't have any problems with one in my previous work. I also couldn't see any difference in the configuration if the file is present or not. With setting httpyac.logLevel=debug you can see in the OutputChannel httpyac.log which proxy is used (search for request)

image

VSCode may also intervene here, as it injects code when the proxy is used. For example, http2 will not work with the default setting of vscode for this reason. You could also test and change the settings there (http.proxySupport). Or use httpyac CLI to test without vscode. It would be a pity if you could not get the file to run. It would be interesting for your own plugins that you might need for your use case.

alekdavisintel commented 1 month ago

I just retried it to compre the logs and it worked fine. Not sure what I did differently. I forgot to select an environment a few times after restarting VSC but I think I did realize it at some point and re-ran (still can't get used to the fact that environment is not remembered between the sessions, and I know there is a way to define the default environment, but I think it's global, and between the projects, environment names may differ, so a global default environment does not really make much sense. and I did not see an option to define project/folder-specific default environment, but maybe I missed it). Anyway, I will keep an eye on it and look at debug logs if I see it again. Thanks.

alekdavisintel commented 1 month ago

Oh, found out that the default environment can be specified in the project config file. AWESOME!!!

AnWeber commented 1 month ago

still can't get used to the fact that environment is not remembered between the sessions

VSCode only lets me save the settings per workspace. I have not yet found an API where I could save it system-wide or per user. I have therefore refrained from implementing it.

alekdavisintel commented 1 month ago

Not a problem. The only irritation was having to set the environment, but since I found how to define a default environment per project, it's less of an issue.

AnWeber commented 1 month ago

Yes, that's my way too. Especially when I realized that I can't remember what the last environment was and so I sent a few not-so-practical requests to production. And as I'm writing this now, it occurs to me that the code could still be there. With httpyac.environmentStoreSelectedOnStart it should save it. You'll have to try it out, but I no longer actively use the path

https://github.com/AnWeber/vscode-httpyac/blob/main/package.json#L520